(转)UITableView的分割线长短的控制

转自:http://blog.csdn.net/smiling8866/article/details/51548234

一、遇到问题

当我利用第三方 FPPopoverController 来显示UITableView的时候,cell的分割线左边没有顶满,而右边却顶满了。这样显示很难看。我需要让其左右两边都是未顶满状态,距离是15像素。

二、解决方法

1、在UITableView的viewDidLoad方法中添加以下代码:

// code1
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
    }

// code2 
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 15, 0, 15)];
    }
 

 2.添加UITableView的一个代理方法:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 15, 0, 15)];
    }
}

三、对解决方法的剖析

code1处代码: 
定制cell分割线的frame

code2处代码: 
-layoutMargins returns a set of insets from the edge of the view’s bounds that denote a default spacing for laying out content. 
If preservesSuperviewLayoutMargins is YES, margins cascade down the view tree, adjusting for geometry offsets, so that setting the left value of layoutMargins on a superview will affect the left value of layoutMargins for subviews positioned close to the left edge of their superview’s bounds 
If your view subclass uses layoutMargins in its layout or drawing, override -layoutMarginsDidChange in order to refresh your view if the margins change.

 

*注意:后来发现了更简单的办法,就是在storyboard那里Seperator insert可以调节

猜你喜欢

转载自jameskaron.iteye.com/blog/2400480