自定义UITableView分隔符插件无法在带有故事板的iOS 8上运行.

我的应用程序的所有tableViews都不遵守storyBoard上的属性SeparatorInset- Custom – left = 0.它在iOS 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];
    }
}

它工作正常,我只是不明白为什么我不能在故事板上设置这个更简单.

有什么想法吗?

解决方法

代码可能对您有所帮助.
包括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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...