美文网首页固予
设置TableView分割线左右对齐

设置TableView分割线左右对齐

作者: ShenYj | 来源:发表于2016-10-21 15:00 被阅读29次

默认TableView每个Cell之间的分割线样式:

OriginalSeparatorStyle.gif

注意左侧,Separator与屏幕左侧存在一定间距,如果我们希望左右两边都对齐屏幕,可以尝试重写viewDidLayoutSubviews方法和实现UITableViewDelegate中的tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath方法:

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

实现效果:

SeparatorStyle.gif

相关文章

网友评论

    本文标题:设置TableView分割线左右对齐

    本文链接:https://www.haomeiwen.com/subject/viqcuttx.html