UITableViewCell的左右边距 [英] Right and left margin to UITableViewCell

查看:43
本文介绍了UITableViewCell的左右边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下UITableViewCells并将背景设置为白色.

I created the following UITableViewCells and set the background to white.

(黑框是模拟器框架).

(the black frame is the simulator frame).

我想在单元格的右边和左边添加边距.

I would like to add margins right and left to the cell.

我查看了许多问答,他们都建议使用UiImageView自定义单元.还有另一种方法吗?

I looked in many Q&A and they all suggested customizing the cell with UiImageView. Is there another way to do it?

推荐答案

添加 UIView 作为单元格的contentView:

add UIView as contentView of the cell:

cellForRowAtIndexPath

UIView *viewLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, heightOfTheCell)];
viewLeft.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:viewLeft];


UIView *viewRight = [[UIView alloc] initWithFrame:CGRectMake(310, 0, 10, heightOfTheCell)];
viewRight.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:viewRight];

这样,您就可以拥有圆角:

and in this way you can have round corners:

[cell.layer setCornerRadius: 5];
[cell.layer setMasksToBounds:YES];
[cell.layer setBorderWidth:2.0f]
CGColorRef colorRed = [[UIColor redColor] CGColor];
[cell.layer setBorderColor:colorRed];

这篇关于UITableViewCell的左右边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆