首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >删除用于Xcode 6 iPhone模拟器的iOS 8 UITableView上的SeparatorInset

删除用于Xcode 6 iPhone模拟器的iOS 8 UITableView上的SeparatorInset
EN

Stack Overflow用户
提问于 2014-09-10 18:10:50
回答 15查看 43.4K关注 0票数 61

我在UITableView上为Xcode6 GM上的iPhone 6模拟器 (iOS 8)找到了一个奇怪的空白区域。我已经尝试从故事板和代码中设置SeparatorInset,但空格仍然存在。

以下代码可以在iOS 7上运行,但不能在iOS 8 (iPhone 6模拟器)上运行。

代码语言:javascript
复制
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
}

我附上了下面的截图:

顺便说一下,我正在使用AutoLayout。我希望有人能告诉我一种方法来移除TableView上奇怪的空白。

EN

回答 15

Stack Overflow用户

回答已采纳

发布于 2014-09-10 19:47:00

感谢学生用注释"Try this self.myTableView.layoutMargins = UIEdgeInsetsZero;“为我指出了正确的方向,这行代码只能在iOS 8上运行,因为layoutMargins只能在iOS 8上使用。如果我在iOS 7上运行相同的代码,它将崩溃。

@property(非原子) UIEdgeInsets layoutMargins描述在视图中布局内容时使用的默认间距。UIView.h引用UIView类引用中声明的Availability iOS (8.0及更高版本)

下面是通过将tableview layoutMarginscell layoutMargins设置为UIEdgeInsetsZero (对于iOS 8)来解决这个奇怪的空格的正确答案。而且它也不会在iOS 7上崩溃。

代码语言:javascript
复制
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

   if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
   }
}

请看下面的屏幕截图:

票数 117
EN

Stack Overflow用户

发布于 2014-09-22 19:27:59

尝试创建一个UITableViewCell类类别并添加此getter

代码语言:javascript
复制
- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsZero;
}

在iOS7中,这不会被调用,因为SDK中没有这个属性,并且不会导致任何崩溃;在iOS8中,每次使用单元格时都会调用这个属性

对我来说很管用

票数 49
EN

Stack Overflow用户

发布于 2014-11-27 16:59:28

我的解决方案只有三行代码:

代码语言:javascript
复制
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)row{
    //
    // ... your code ...
    //
    if ([cell respondsToSelector:@selector(preservesSuperviewLayoutMargins)]){
        cell.layoutMargins = UIEdgeInsetsZero;
        cell.preservesSuperviewLayoutMargins = false;
    }
    return cell;
}
票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25762723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档