Genevios
Genevios

Reputation: 1145

Detect last cell on each section in tableView

i need your help to find best solution to solve my problem. I have dynamic data(JSON parse) with TableView. My tableView consist of some sections(also parse from JSON). I must detect the last cell in each section and after customize it just add round corner. (like new notification view on iOS 10)

So i already tried this code:

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

With this part of code detect and round corner only the last cell in all tableview no the last in each section...Please help anyone.

Solution:(work for me perfectly)

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

    } 
}

And forbidden reload cell

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

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

}

Upvotes: 1

Views: 2623

Answers (1)

user5890979
user5890979

Reputation:

You should not look against the existing tableView sections etc.

Since you don't need to do this for cells and sections that are not visible on the screen.

The best way is to check against your datasource that holds the information to each section.

This check should instead be done in 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:.

EDIT:

I misunderstood your question.

Here is the check you can do in your method described above:

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

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

}

Or to check against your datasource which is cleaner in my opinion as I described above:

NSInteger numberOfRows = myDataSourceNumberOfRowsForIndexPathSection;

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

}

Upvotes: 3

Related Questions