How to customize tableView separator in iPhone

前端 未结 12 1987
夕颜
夕颜 2020-11-28 20:49

By default there is a single line separator in uitableview.

But I want to put my customized line as a separator.

Is it possible? How?

12条回答
  •  情话喂你
    2020-11-28 21:48

    These answers result in the highlight rect being overwritten by the separator you add to your cell (on iOS 6 with my testing at least). You need to set the separatorColor to [UIColor clearColor] so that cells are still separated by 1px, you can then draw your separator in the gap:

    - (void)viewDidLoad {
        self.tableView.separatorColor = [UIColor clearColor];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // snip
        CALayer *separator = [CALayer layer];
        separator.backgroundColor = [UIColor redColor].CGColor;
        separator.frame = CGRectMake(0, 43, self.view.frame.size.width, 1);
        [cell.layer addSublayer:separator];
        return cell;
    }
    

提交回复
热议问题