我有一个包含 3 个部分的 UITableView,每个部分返回 3 个单独数组中的 numberOfRows 计数。在我的 cellForRow 中,我需要能够检测哪个单元格是表中的第一个单元格,哪个是最后一个单元格。
棘手的部分是,有时一个部分可能会返回 0 的行数,所以我很难弄清楚这一点。我想做的就是设置两个标志:isTopCell
和isBottomCell
。有什么想法吗?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section==0)
return [array1 count];
else if(section==1)
return [array2 count];
else return [array3 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(/*something, what I'm trying to figure out*/)
isTopCell=YES;
if(/*something, what I'm trying to figure out*/)
isBottomCell=YES;
}
编辑:让我澄清一下。 3个部分是统一的。我想确定整体顶部和底部单元格。 if(indexPath.row==0) 将为 3 个部分返回 true。我只需要 1 位获胜者。
最佳答案
试试这个:
int index = indexPath.row;
if (indexPath.section > 0) index += [array1 count];
if (indexPath.section > 1) index += [array2 count];
if (index == 0) // Top row
if (index == [array1 count] + [array2 count] + [array3 count] - 1) // Bottom row
关于iphone - 确定第一个和最后一个 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9944191/