gpt4 book ai didi

iphone - 如何知道单元格是否位于部分中的第一个

转载 作者:行者123 更新时间:2023-12-03 20:45:04 25 4
gpt4 key购买 nike

自定义 UITableView 时,如何知道该单元格是该部分中的第一个、最后一个还是唯一一个

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

方法?

谢谢。

最佳答案

要查明它是否是节中的最后一行,请在数据源委托(delegate)上调用 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
}

关于iphone - 如何知道单元格是否位于部分中的第一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8980910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com