Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jiance Tong
3,367 PointsHow to add the section title in the tableview
first I try to use the following method:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; but it turns out not work, and I didn't find the method in Xcode5's documentation. But I find another method, called: headerViewForSection. When I use it, it still not work. Anyone can help me? Thanks!
2 Answers
Dennis Dixon
6,107 PointsHi Jiance,
If you're looking to populate the section header with a simple NSString, the UITableViewDataSource method you're using (titleForHeaderInSection:) should work. I tested it using Xcode 5.1, the code below, and it worked fine:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"Test Header"; }
I would make sure the view controller class you're putting the method in is the one being pointed to as the table view's data source. Assuming you're using a storyboard, you can use the Connections inspector to do that. Just select the table view (not the table view controller) and, in the "Outlets" section, make sure the name of your UITableViewController subclass is connected to the 'dataSource'. In most cases, it should also be connected to the 'delegate' as well.
The Apple Doc is at the following URL: https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView:titleForHeaderInSection:
Hope this helps, Dennis
Dennis Dixon
6,107 PointsGlad to help, Jiance!
In general, code complete won't work unless the method has been declared through import of a header file or an explicit declaration in the private interface section of the .m file. However, even when that's been done (as was the case with you in the import of the UIKit and making your view controller a subclass of UITableViewController), Xcode gets a little out of whack. In those cases, a 'clean' usually makes it all better. Next time, in Xcode try "Product>Clean".
Regards, Dennis
Jiance Tong
3,367 PointsJiance Tong
3,367 PointsThanks for the help, Dennis. I try it again, this time it works. Hmm, it is a little bit strange. Last time Xcode even cannot recognize the method. But when I try to compile it, it didn't throw an error. interesting. Thanks for the detailed answer.