开发者

Give each cell an unique tag (incremental number)

Is there a way to give each cell in a grouped tableview an incremental number as its tag?

E.g:

Group 1

Group 2

  • cell 1 (tag = 4)
  • cell 2 (tag = 5)

Group 3

  • cell 1 (tag = 6)

etc...

Any help is greatly appreciated!


This is not possible without querying the datasource for the count of cells that are in previous groups. And you probably don't want to do that. Doesn't make sense anyway, because you have to implement proper reuse to get good performance, so tags appear and disappear any time.

So the real question is, why do you want to do this? There is probably a way to achieve the same without adding tags.

But if you really want to:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger count = 1;
    for (NSInteger i = 0; i < indexPath.section; i++) {
        count += [[tableView dataSource] tableView:tableView numberOfRowsInSection:i];
    }
    count += indexPath.row;
    // dequeue, create and configure...
    cell.tag = count;
    return cell;
}


  1. Create a variable
  2. Create a loop which will iterate through all cells
  3. At the end of each loop add 1 to the variable
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜