常用代码块

#pragma mark - <UITableViewDataSource, UITableViewDelegate>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *const cellId = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    cell.textLabel.text = @"通过xib约束设置:edge(0, 0, 0, 0),\n顶部有64的间距";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.navigationController pushViewController:[ViewController2 new] animated:YES];
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    }
    return _tableView;
}

UITableView 适配iOS 11 设置

+ (UITableView *)tableViewOfGroupedAndSetDelegate:(id)delegate {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero
                                                          style:UITableViewStyleGrouped];
    tableView.delegate = delegate;
    tableView.dataSource = delegate;
    tableView.backgroundColor = LGColorBackgroundColorGray;
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;

    // 适配iOS11:两项必须设置
    tableView.sectionHeaderHeight = 0;
    tableView.sectionFooterHeight = 0;

    if (@available(iOS 11.0, *)) {
        // 发现第一个 section 顶部会多出一部分的距离(tableHeaderView 占据的空间,即使没有设置)
        // 设置一个高度很小的 tableHeaderView ,注意:高度不能设置为 0,否则不起作用
        tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        tableView.estimatedSectionHeaderHeight = 0.f;
    }

    return tableView;
}

注意:

UITableView为 UITableViewStyleGrouped 类型时,必须需要以下方法方法返回真实高度,如果是 0,使用 CGFLOAT_MIN

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
 return CGFLOAT_MIN;
 }
 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
 return CGFLOAT_MIN;
 }

去掉多余的的cell

self.tabelView.tableFooterView = [UIView new];

去掉分割线:点击的时候,分割线也不会出现

_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

移除iOS7之后,cell默认左侧的分割线边距

#define kRemoveCellSeparator \
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{\
cell.separatorInset = UIEdgeInsetsZero;\
cell.layoutMargins = UIEdgeInsetsZero; \
cell.preservesSuperviewLayoutMargins = NO; \
}

results matching ""

    No results matching ""