UITableView如何让cell的分割线左边不缩进

http://blog.csdn.net/wei78008023/article/details/44922825


在ios7中,UITableViewCell左侧会有默认15像素的空白。这时候,设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉。

但是在ios8中,设置setSeparatorInset:UIEdgeInsetsZero 已经不起作用了。


可以用以下方法去除左边的默认缩进:


1.首先在viewDidLoad方法加入以下代码:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.  if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {  
  2.   
  3. [self.tableView setSeparatorInset:UIEdgeInsetsZero];  
  4.   
  5. }  
  6.   
  7. if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {  
  8.   
  9. [self.tableView setLayoutMargins:UIEdgeInsetsZero];  
  10.   
  11. }  

2. 然后在UITableView的代理方法中加入以下代码

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.   
  4. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {  
  5. [cell setSeparatorInset:UIEdgeInsetsZero];  
  6.   
  7. }  
  8.   
  9. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {  
  10. [cell setLayoutMargins:UIEdgeInsetsZero];  
  11. }  
  12. }  


当然,这样做看起来比较麻烦,也会让代码变得不那么简洁,所以建议干脆就把分割线直接隐藏,然后自定义cell的时候自己加上上下边线,这样自由度比较高,想怎修改就完全看自己的喜好了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值