How can I simply insert a date header when row is a different day on iOS?
I have a UITableView, where I'm adding data to a row using a nib file. I've just showing the the table, nothing happens if the user taps the row, its just a report.
However, I have lots of rows and some rows occur on the same date, I'd like to add a thin row to group these rows.
Can I do this within cellForRowAtIndexpath, if not how?
Here's my current code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ReportCell";
ReportCell *cell = (ReportCell *开发者_开发技巧) [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:@"ReportCell"
owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = ((ReportCell *) currentObject);
break;
}
}
}
The best way to do this is probably with sections.
Take a look at
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
UITableView supports sections with headers.
This question/answer discuss how to use sectioned table views backed by Core Data:
Core Data backed UITableView with indexing
精彩评论