开发者

tableView section based on string

开发者 https://www.devze.com 2023-03-14 16:02 出处:网络
I\'m looking for something like this Tableview section By Date instead of a date, I have a NSMutableArray that contains information like...

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消