iphone - 确定第一个和最后一个 UITableViewCell

标签 iphone objective-c ios uitableview

我有一个包含 3 个部分的 UITableView,每个部分返回 3 个单独数组中的 numberOfRows 计数。在我的 cellForRow 中,我需要能够检测哪个单元格是表中的第一个单元格,哪个是最后一个单元格。

棘手的部分是,有时一个部分可能会返回 0 的行数,所以我很难弄清楚这一点。我想做的就是设置两个标志:isTopCellisBottomCell。有什么想法吗?

- (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/

相关文章:

iphone - 从 NSData 或 UIImage 查找图像类型

objective-c - 自动释放或不自动释放

iphone - 自定义 View 中 subview 布局的自定义方法,或使用layoutSubviews/layoutIfNeeded

android - Jetpack Compose 内部设计 : Why draw on a Canvas, 而不是重用原生 Views?

CSS 链接不适用于响应式设计

iphone - 使用 moya_modelMapper 映射 moya 结果

iphone - 在 UIScrollView 上添加 UITextView/UILabel

ios - UISlider 不更新

ios - 滚动时保持 UITableViewCell 可见? ( swift 3)

iphone - 不需要的双导航栏