iOS UITableViewCell的一些事

//可以去掉分割线
- (void)addSubview:(UIView *)view{
    if ([view isKindOfClass:NSClassFromString(@"_UITableViewCellSeparatorView")]) {
        return;
    }
    [super addSubview:view];
}
//改变cell的frame
- (void)setFrame:(CGRect)frame{
    /*
     左右距离 10 ,y+10,高度减少15
     */
    if (frame.size.height < 1) {//直接==0 在Xcode11下编译有bug
        [super setFrame:frame];
        return;
    }
    frame.origin.x = 15;
    frame.size.width -= 2 * frame.origin.x;
//    frame.origin.y += 10;
    frame.size.height -= 20;
    [super setFrame:frame];
}
//取消点击效果
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//简单修改UITableViewHeaderFooterView的字体、颜色等
//新建个类继承UITableViewHeaderFooterView,然后在layoutSubviews里修改
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.textLabel.font = [UIFont boldSystemFontOfSize:18];
    if (@available(iOS 13.0, *)) {
        self.textLabel.textColor = [UIColor labelColor];
    } else {
        self.textLabel.textColor = [UIColor blackColor];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   ...
    
    cell.preservesSuperviewLayoutMargins = NO;///>=iOS 8,子view和父view间有8个像素
    cell.layoutMargins = UIEdgeInsetsMake(0, 20, 0, 20);///>=iOS 8
    cell.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);///<=iOS 7,默认距左15
    
    return cell;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容