I'm looking for something like this Tableview section By Date
instead of a date, I have a NSMutableArray that contains information like...
14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16... etc (all in string)
I would like to sectio开发者_JS百科n them based on those strings (which are days of the month). no need to rearrange.
Whilst I am not sure if this is exactly what you want, let's assume that your array is name numbers
.
NSCountedSet * numberSet = [[NSCountedSet alloc] initWithArray:numbers];
NSArray * sortedSections = [[[numberSet allObjects] sortedArrayUsingSelector:@selector(compare:)] retain];
Now,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [sortedSections count];
}
- (NSInteger)tableView:(UITableView *)tableVIew numberOfRowsInSection:(NSInteger)section {
return [numberSet countForObject:[sortedSections objectAtIndex:section]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[..]
cell.textLabel.text = [sortedSections objectAtIndex:indexPath.section];
return cell;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return sortedSections;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sortedSections objectAtIndex:section];
}
Let me know if you need something else.
精彩评论