How to recreate the default UIView in the same way as the default tableView: viewForHeaderInSection :?

I tried to implement

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

to get the title text label in the section of black instead of white, but it looks so different from the standard created by the SDK, mine is so ugly.

How to recreate a UIView in the same way as with the SDK?

From the Apple documentation:

Discussion
The table view uses a fixed font style for section heading headers. If you need a different font style, return a custom view (for example, a UILabel object) in the delegate method tableView: viewForHeaderInSection: instead.

+7
objective-c iphone uitableview
source share
4 answers

Although this is not entirely correct, I am posting this in the hope that someone will be able to improve my approach. The text is right (white, as by default, but you can change it). The background gradient and opacity are not perfect (too light), some adjustment may be required.

Get sectionheaderbackground.png here: http://img5.imageshack.us/img5/6616/sectionheaderbackground.png

 // Create a stretchable image that emulates the default gradient UIImage *buttonImageNormal = [UIImage imageNamed:@"sectionheaderbackground.png"]; UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0]; // Create the view for the header CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, 22.0); UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame]; sectionView.alpha = 0.9; sectionView.backgroundColor = [UIColor colorWithPatternImage:stretchableButtonImageNormal]; // Create the label CGRect labelFrame = CGRectMake(10.0, 0.0, 310.0, 22.0); UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame]; sectionLabel.text = @"Test section label"; sectionLabel.font = [UIFont boldSystemFontOfSize:18.0]; sectionLabel.textColor = [UIColor whiteColor]; sectionLabel.shadowColor = [UIColor grayColor]; sectionLabel.shadowOffset = CGSizeMake(0, 1); sectionLabel.backgroundColor = [UIColor clearColor]; [sectionView addSubview:sectionLabel]; [sectionLabel release]; // Return the header section view return sectionView; 
+13
source share

You can get the default UITableView section headings by running the following methods in the tableview class:

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return nil; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 22; //returning 0 hides section headers } 

This can be especially useful if you are using the UISearchDisplayController in your application where you want the search results to be displayed in the simple way of the UITableViewStylePlain , rather than, say, the UITableViewStyleGrouped your customized main table view. Then you can implement something like this:

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if ([self.searchDisplayController.searchBar isFirstResponder]) return nil; //create and return your custom UIView here } 
+1
source share

You can try to simulate it using shadowColor and shadowOffset .

I did it in black, for example, using the following code:

 label.shadowColor = [UIColor whiteColor]; label.shadowOffset = CGSizeMake(1, 1); 

Combined with combinations of colors and shadow, you can imitate the shortcut well.

0
source share

If you want to recreate the default / standard heightForHeaderInSection , use this:

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 44; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 11, tableView.frame.size.width-100, 21)]; label.text = @"Text"; label.textColor = [UIColor colorWithRed:76.0/255 green:86.0/255 blue:108.0/255 alpha:1]; label.backgroundColor = [UIColor clearColor]; label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0]; [view addSubview:label]; return view; } 
0
source share

All Articles