我在UITableView
上为Xcode6 GM上的iPhone 6模拟器 (iOS 8)找到了一个奇怪的空白区域。我已经尝试从故事板和代码中设置SeparatorInset
,但空格仍然存在。
以下代码可以在iOS 7上运行,但不能在iOS 8 (iPhone 6模拟器)上运行。
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
}
我附上了下面的截图:
顺便说一下,我正在使用AutoLayout。我希望有人能告诉我一种方法来移除TableView
上奇怪的空白。
发布于 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 layoutMargins
和cell layoutMargins
设置为UIEdgeInsetsZero
(对于iOS 8)来解决这个奇怪的空格的正确答案。而且它也不会在iOS 7上崩溃。
-(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];
}
}
请看下面的屏幕截图:
发布于 2014-09-22 19:27:59
尝试创建一个UITableViewCell类类别并添加此getter
- (UIEdgeInsets)layoutMargins {
return UIEdgeInsetsZero;
}
在iOS7中,这不会被调用,因为SDK中没有这个属性,并且不会导致任何崩溃;在iOS8中,每次使用单元格时都会调用这个属性
对我来说很管用
发布于 2014-11-27 16:59:28
我的解决方案只有三行代码:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)row{
//
// ... your code ...
//
if ([cell respondsToSelector:@selector(preservesSuperviewLayoutMargins)]){
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = false;
}
return cell;
}
https://stackoverflow.com/questions/25762723
复制相似问题