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

后端 未结 14 1019
礼貌的吻别
礼貌的吻别 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: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 == -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];
     }
    

提交回复
热议问题