自定义UITableView分隔符插入在带有情节串连板的iOS 8上不起作用



我的应用程序的所有tableViews都不尊重storyBoard上的property SeparatorInset-Custom-left = 0iOS 7上一切正常,但现在不行了。

当我实现以下两种方法时:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // iOS 7 
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    // iOS 8 
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(void)viewDidLayoutSubviews
{
    // iOS 7 
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    // iOS 8 
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

它工作正常,我只是不明白为什么我不能在简单得多的storyboard上继续设置它。

有什么想法吗?

此代码可能会对您有所帮助。包括layoutMargins和separatorInset,以支持两个iOS版本7,8对于iOS8:

首先配置您的表视图如下:

if ([self.tableView respondsToSelector:@selector(layoutMargins)]) {
    self.tableView.layoutMargins = UIEdgeInsetsZero;
}

然后在您的cellForRowAtIndexPath:方法中,按如下方式配置单元格:

if ([cell respondsToSelector:@selector(layoutMargins)]) {
    cell.layoutMargins = UIEdgeInsetsZero;
}

对于版本检查,您可以使用以下内容:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
  if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
    cell.layoutMargins = UIEdgeInsetsZero;
  }
#endif
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    // Remove separator inset
    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset = UIEdgeInsetsZero
    }
    // Prevent the cell from inheriting the Table View's margin settings
    if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
        if #available(iOS 8.0, *) {
            cell.preservesSuperviewLayoutMargins = false
        }
    }
    // Explictly set your cell's layout margins
    if cell.respondsToSelector("setLayoutMargins:") {
        if #available(iOS 8.0, *) {
            cell.layoutMargins = UIEdgeInsetsZero
        }
    }
}

简单:

cell.separatorInset=UIEdgeInsetsMake(0.0,CGRectGetWidth(tableView.frame)/2,0.0,CG矩形宽度(tableView.frame)+2);

最新更新