UITableView: the proper way to display a separator for the last cell

后端 未结 14 929
礼貌的吻别
礼貌的吻别 2020-12-29 02:23

The question is what\'s the right-most way to display a separator in the last cell in a table/section.

Basically this is what I am after.

相关标签:
14条回答
  • 2020-12-29 03:10

    So here's a super simple solution in Swift 4 which works:

    Inside override func viewDidLoad(){}, I simply implemented this line of code:

        self.tableView.tableFooterView = UIView(frame: .zero)
    

    Hence it ensures that only the last cell gets the separator inset.

    This worked perfectly for me, hope it does for you too!

    0 讨论(0)
  • 2020-12-29 03:12

    Works in iOS 7.x and 8.x (put it in cellForRowAtIndexPath):

    (PS substitute "max elements of your datasource" with your array datasource count)

     if (row == <max elements of your datasource>-1) {
         UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height-1, self.view.frame.size.width, 1)];/// change size as you need.
         separatorLineView.tag = 666;
         separatorLineView.backgroundColor = [UIColor colorWithRed: 204.0/255.0 green: 204.0/255.0 blue: 204.0/255.0 alpha:1.0];
         UIView *separator = [cell.contentView viewWithTag:666];
         // case of orientation changed..
         if (separator.frame.size.width != cell.contentView.frame.size.width) {
            [[cell.contentView viewWithTag:666] removeFromSuperview];
            separator = nil;
         }
         if (separator==nil) [cell.contentView addSubview:separatorLineView];
     }
    
    0 讨论(0)
  • 2020-12-29 03:12

    The code below worked for me:

    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 0.35)];
    footerView.backgroundColor = [UIColor whiteColor];
    CGRect frame = footerView.frame;
    frame.origin.x = 15;
    frame.size.width = frame.size.width - 15;
    UIView *blackView = [[UIView alloc] initWithFrame:frame];
    [blackView setBackgroundColor:[UIColor blackColor]];
    blackView.alpha = 0.25;
    [footerView addSubview:blackView];
    tableView.tableFooterView = footerView;
    

    Hope it works for you too.

    0 讨论(0)
  • 2020-12-29 03:13

    This worked flawlessly for me to add a separator to the last cell. have fun!

    self.tableView.tableFooterView = [[UIView alloc] init];
    
    0 讨论(0)
  • 2020-12-29 03:13

    This is definitely help. Working. but set separator "none" from attribute inspector. Write following code in cellForRowAtIndexPath method

    if(indexPath.row==arrData.count-1){
     UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,           
     cell.contentView.frame.size.height - 1.0,      
     cell.contentView.frame.size.width, 1)];
    
        lineView.backgroundColor = [UIColor blackColor];
        [cell.contentView addSubview:lineView];
    }
    
    0 讨论(0)
  • 2020-12-29 03:17

    this will do exactly what you want .. even though the line will take the entire width of the cell:

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

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor = [UIColor redColor];
    self.tableView.separatorInset = UIEdgeInsetsZero;
    
    0 讨论(0)
提交回复
热议问题