检测 tableView 中每个部分的最后一个单元格

Detect last cell on each section in tableView

我需要你的帮助来找到解决我的问题的最佳方案。 我有 TableView 的动态数据(JSON 解析)。我的 tableView 由一些部分组成(也从 JSON 解析)。我必须检测 每个部分的最后一个单元格 并在自定义后添加圆角。 (比如 iOS 10 上的新通知视图)

所以我已经尝试过这个代码:

NSInteger sectionsAmount = [tableView numberOfSections];
NSInteger rowsAmount = [tableView numberOfRowsInSection:[indexPath section]];
if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) 

使用这部分代码检测和圆角只有所有表格视图中的最后一个单元格没有每个部分中的最后一个...请帮助任何人。

解决方案:(非常适合我)

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];

    if (indexPath.row == (numberOfRows-1)) {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:( UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];

        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.view.bounds;
        maskLayer.path  = maskPath.CGPath;
        cell.tag = 1;
        cell.layer.mask = maskLayer;

    } 
}

并禁止重载传感器

-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell = (ScheduleCell*)[cell viewWithTag:1];
    [cell removeFromSuperview];

}

你不应该对照现有的 tableView 部分等等

因为您不需要对屏幕上不可见的单元格和部分执行此操作。

最好的方法是对照您的 datasource 检查每个部分的信息。

这项检查应该在 tableView:cellForRowAtIndexPath: method. You check if the cell indexPath.row is the last cell of the dataSource holding the information to your sections , and do your customizations in there or in tableView:willDisplayCell:forRowAtIndexPath: 中完成。

编辑:

我误解了你的问题。

这是您可以在上述方法中执行的检查:

NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];

if (indexPath.row == (numberOfRows-1)) {

}

或者检查你的数据源,我认为哪个更干净,如上所述:

NSInteger numberOfRows = myDataSourceNumberOfRowsForIndexPathSection;

if (indexPath.row == (numberOfRows-1)) {

}