怎样让UITableView的ios cell分割线左边距从最左边开始绘制

拒绝访问 | www.she.vc | 百度云加速
请打开cookies.
此网站 (www.she.vc) 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(38be9-ua98).
重新安装浏览器,或使用别的浏览器iOS应用开发中UITableView的分割线的一些设置技巧
作者:ForeverYoung21
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了iOS应用开发中UITableView分割线的一些设置技巧,包括消除分割线的方法,示例代码为传统的Objective-C语言,需要的朋友可以参考下
对于ios7,ios8及以上来说,调整UITableView的cell的分割线位置已经是相当不便,因为UITableView内部使用了margin layout.
其实只需要如下这样子就可以实现分割线的控制。
-(void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath
&&& // 下面这几行代码是用来设置cell的上下行线的位置
&&& if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
&&& [cell setLayoutMargins:UIEdgeInsetsZero];
&&& //按照作者最后的意思还要加上下面这一段,才能做到底部线控制位置,所以这里按stackflow上的做法添加上吧。
&&& if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
&&&&&&& [cell setPreservesSuperviewLayoutMargins:NO];
如果要直接使用TableView的sectionTitle,但又想设置它的字体,颜色什么的,可以使用如下方法。
- (void)tableView:(UITableView )tableView willDisplayHeaderView:(UIView )view forSection:(NSInteger)section
// Background color
view.tintColor = [UIColor blueColor];
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)
[header.textLabel setTextColor:[UIColor redColor]];
// 另一种方法设置背景颜色
// header.contentView.backgroundColor = [UIColor blackColor];
不显示分割线
通过tableFooterView修改UITableView分割线:
在使用UITableView的时候,如果没有数据/数据很少,会发现即使没有数据的cell也会有分割线,这样看起来并不美观,通常我们希望只有显示数据的cell会显示对应的分割线,而不显示数据的cell不显示分割线。
常用的做法有两种:
第一种做法是首先取消显示分割线,然后自定义cell,在cell的最底部加上一个高度为1的view,这样看起来就像是一条分割线。只有cell有数据显示出来的时候才会显示这个view,这样就达到了目的。
第二种做法既不用取消显示分割线,也不需要自定义cell,而是直接这样做:
self.tableView.tableFooterView = [[UIView alloc] init];
运行显示结果,发现就已经达到了我们的目的。很明显这种做法更方便。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具设置UITableView分割线左边间距 - 简书
设置UITableView分割线左边间距
iOS 7之后,tableView的分割线左边距默认设置为15,如图
左间距15的分割线.png
iOS 7 添加属性separatorInset,设置为UIEdgeInsetsZero即可左对齐iOS 8之后,由于view添加属性layoutMargins,所以需要两个属性都设置为 UIEdgeInsetsZero
1.代码设置
首先在- viewDidLayoutSubviews方法里设置tableView的separatorInset和layoutMargins//OC
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
//设置separatorInset(iOS7之后)
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
//设置layoutMargins(iOS8之后)
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if self.tableView.respondsToSelector(Selector("setSeparatorInset:")) {
self.tableView.separatorInset = UIEdgeInsetsZero
if self.tableView.respondsToSelector(Selector("setLayoutMargins:")) {
self.tableView.layoutMargins = UIEdgeInsetsZero
在- tableView:willDisplayCell:forRowAtIndexPath:方法(cell将要出现)里设置cell的separatorInset和layoutMargins//OC
- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
//设置separatorInset(iOS7之后)
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
//设置layoutMargins(iOS8之后)
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector(Selector("setSeparatorInset:")) {
cell.separatorInset = UIEdgeInsetsZero
if cell.respondsToSelector(Selector("setLayoutMargins:")) {
cell.layoutMargins = UIEdgeInsetsZero
2.Xib/Storyboard设置
需要同时设置tableView和cell的separatorInset和layoutMargins.(针对tableView和tableViewController的xib和storyboard不同情况,选择性设置即可,最多4个都设置)
首先设置TableView的separatorInset,如图
设置TableView的SeparatorInset.png
设置TableView的layoutMargins,如图
设置TableView的LayoutMargins.png
设置TableViewCell的separatorInset,如图
设置Cell的SeparatorInset.png
设置TableViewCell的layoutMargins,如图
设置Cell的LayoutMargins.pngios 设置表格tableview每行分割线从屏幕边缘开始_IOS开发-织梦者
当前位置:&>&&>& > ios 设置表格tableview每行分割线从屏幕边缘开始
ios 设置表格tableview每行分割线从屏幕边缘开始
-(void)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];
以上就是ios 设置表格tableview每行分割线从屏幕边缘开始的全文介绍,希望对您学习和使用ios应用开发有所帮助.
这些内容可能对你也有帮助
更多可查看IOS开发列表页。
猜您也会喜欢这些文章

我要回帖

更多关于 tableview 分割线左边 的文章

 

随机推荐