如何知道细胞是否是第一部分

时间:2012-01-24 01:11:53

标签: iphone uitableview

在自定义UITableView时,如何知道该单元格是

部分中的第一个,最后一个还是唯一一个
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath` 

方法

感谢。

2 个答案:

答案 0 :(得分:3)

要查明它是否是某个部分的最后一行,请在数据源代理上调用tableView:numberOfRowsInSection:(可能是self,因为您使用的是tableView:cellForRowAtIndexPath:方法。如果从该方法返回的值等于indexPath.row+1,则表示您位于最后一行。如果另外indexPath.row == 0,您就是唯一的一行。如果indexPath.row == 0tableView:numberOfRowsInSection:返回的数字不是1,则表示您正在查看某个部分的第一行。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger count = [self tableView:tableView numberOfRowsInSection:indexPath.section];
    BOOL first = indexPath.row == 0;
    BOOL last = indexPath.row+1 == count;
    if (first && last) {
        NSLog(@"The only cell in section %d", indexPath.section);
    } else if (first) {
        NSLog(@"The first cell in section %d", indexPath.section);
    } else if (last) {
        NSLog(@"The last cell in section %d", indexPath.section);
    } else {
        NSLog(@"Nothing special...");
    }
    // Do the rest of your work
}

答案 1 :(得分:1)

NSIndexPath拥有您需要的所有信息。行和部分。