UITableViewCell分隔線補(bǔ)全方法

在使用tableView的時(shí)候發(fā)現(xiàn), cell分割線右邊有一點(diǎn)缺失.總結(jié)三個(gè)補(bǔ)全方法,使cell的分割線補(bǔ)全:
1 添加一個(gè)自動(dòng)以view

實(shí)現(xiàn)步驟:

  • 取消系統(tǒng)的分隔線
  • 自定義cell, 在layOutsubviews添加自定義分隔線
#import "ViewController.h"
#import "JNCell.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 取消系統(tǒng)的分隔線
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pargma mark --自定義cell
#import "JNCell.h"
@implementation JNell

- (void)layoutSubviews {

    [super layoutSubviews];

    // 初始化數(shù)據(jù)
    CGFloat w = self.frame.size.width;
    CGFloat h = 1;
    CGFloat x = 0;
    CGFloat y = self.frame.size.height - 1;
    // 添加自定義分隔線
    UIView *sepLine = [[UIView alloc] init];

    sepLine.frame = CGRectMake(x, y, w, h);

    // 設(shè)置背景色
    sepLine.backgroundColor = [UIColor colorWithRed:150/255.0 green:150/255.0  blue:150/255.0  alpha:1];

    [self.contentView addSubview:sepLine];
}
@end
2 在TableView中所在的方法中添加相應(yīng)的方法

注意 :
該方法在iOS7.0系統(tǒng)及以下會(huì)報(bào)錯(cuò);因?yàn)閘ayoutMargins屬性是8.0才有的

- (void)viewDidLayoutSubviews {

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {

        self.tableView.layoutMargins = UIEdgeInsetsZero;
    }
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {

        self.tableView.separatorInset = UIEdgeInsetsZero;
    }
}

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

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

        cell.layoutMargins = UIEdgeInsetsZero;
    }

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        cell.separatorInset = UIEdgeInsetsZero;
    }
}
3 通過(guò)背景顏色偷梁換柱

實(shí)現(xiàn)方法:

  • 取消系統(tǒng)的分隔線
  • 設(shè)置tableView的背景顏色
  • 自定義cell, 重寫(xiě)系統(tǒng)的setFrame:方法
#import "ViewController.h"
#import "JNCell.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1.0 取消系統(tǒng)的分隔線
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    // 2.0 設(shè)置背景顏色
    self.tableView.backgroundColor = [UIColor grayColor];
}

#pargram mark -- 自定義cell
#import "JNCell.h"
@implementation ABCell

// 重寫(xiě)系統(tǒng)方法
- (void)setFrame:(CGRect)frame {
    // 設(shè)置分隔線的高度
    frame.size.height -= 1;
    // 調(diào)用系統(tǒng)的方法
    [super setFrame:frame];
}

/**
當(dāng)系統(tǒng)自動(dòng)計(jì)算好cell的尺寸后, 會(huì)調(diào)用該方法給cell設(shè)置frame

這時(shí)候重寫(xiě)setframe方法, 把cell的高度減1; 故顯示后會(huì)露出高度為1(tableView的)背景顏色 (充當(dāng)分隔線)
*/
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 從iOS7.0開(kāi)始, tableViewCell的分隔線總會(huì)在左側(cè)留意下一點(diǎn)小間距, 這是蘋(píng)果更新系統(tǒng)之后鬧出的幺...
    面糊閱讀 1,328評(píng)論 0 50
  • 1.創(chuàng)建一個(gè)UITableView對(duì)象 ITableView *tableView = [[UITableView...
    桐生一貓閱讀 1,496評(píng)論 0 5
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,502評(píng)論 19 139
  • 廢話不多說(shuō),直接上干貨 ---------------------------------------------...
    小小趙紙農(nóng)閱讀 3,645評(píng)論 0 15
  • 一禪冷月吳鉤,如來(lái)幾多愁? 低眉垂眸,大千世界蓮出污垢。 也曾癡怨也曾修,累世千劫情難夠, 誰(shuí)又能載得動(dòng)這許多寂寥秋……
    易郁生閱讀 549評(píng)論 2 2

友情鏈接更多精彩內(nèi)容