UITableView使用过程中可能遇到的问题

前言:记录一些UITableView使用过程中可能遇到的问题

环境:Xcode9

  • 解决UITableViewStyleGrouped类型的TableView的cell距离顶部有距离的问题:
1 tableV.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
  • 解决分割线相关的问题
    • 方法1:对UITableView下手:这种方法是一种官方挑明的处理方式
1 if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
2         //Specifies the default inset of cell separators.
3         //该方法是在iOS7之后新增的属性之前都是分割线一直到边的后来的时候才是需要人为地去设置分割线到边 In iOS 7 and later, cell separators do not extend all the way to the edge of the table view. This property sets the default inset for all cells in the table,
4         [tableV setSeparatorInset:UIEdgeInsetsZero];
5 }
    • 方法2:对Cell下手:这种方式是考虑到了其实那些分割线是cell的子视图
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
        /*
         The inset values for the cell’s content.
         You can use this property to add space between the current cell’s contents and the left and right edges of the table. Positive inset values move the cell content and cell separator inward and away from the table edges. Negative values are treated as if the inset is set to 0.
         cell的内容边距
         为了添加在当前的cell的内容与table左右的边距 可以设置这个属性
         正值是移动Cell的内容相对table边距 向里或者是远离 向里边收
         负值是被看作是0一样
         */
    }

 思考:其实为什么设置了这个属性就能够起作用呢,打印cell.subViews

1 -[WWLearnViewController tableView:cellForRowAtIndexPath:] [Line 90] (
2 
3      "<UITableViewCellContentView: 0x7ff69b407420; frame = (0 0; 414 44.6667); gestureRecognizers = <NSArray: 0x60400025d760>; layer = <CALayer: 0x60400002ffa0>>",
4 
5      "<_UITableViewCellSeparatorView: 0x7ff69b70f430; frame = (0 44.6667; 414 0.333333); layer = <CALayer: 0x60000002b100>>",
6 
7      "<_UITableViewCellSeparatorView: 0x7ff69b70f880; frame = (0 0; 414 0.333333); layer = <CALayer: 0x60000002b220>>"
8 
9      )

  可以发现:

 _UITableViewCellSeparatorView这个视图应该就是tableView的分割线视图

 相关的验证可以在自定义的cell的时候,可以利用runtime相关的只是获取到私有分隔线属性名字,然后通过KVC的方法获取到cell的私有属性的分隔线视图,并且改变其颜色得到

没有赋值的Cell这会儿是默认的Cell的高度44 point,而且宽度都还是320,不知道这个320是不是 iPhone 2G, 3G, 3GS,iPhone 4, 4s,iPhone 5, 5s, 5c, SE之前的传统问题

"<UITableViewCellContentView: 0x7fabd6d22ca0; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0x60400025a610>; layer = <CALayer: 0x604000428020>>"

简单赋值的没有改变行高的Cell

 "<UITableViewCell: 0x7fabd8076a00; frame = (0 710; 414 45); text = 'textLabel'; autoresize = W; layer = <CALayer: 0x6040004294c0>>"
 "<UITableViewCellContentView: 0x7fabd6d23e30; frame = (0 0; 414 44.6667); gestureRecognizers = <NSArray: 0x604000258a20>; layer = <CALayer: 0x604000429420>>",
    "<_UITableViewCellSeparatorView: 0x7fabd6c0c650; frame = (0 44.6667; 414 0.333333); layer = <CALayer: 0x6040004297c0>>"

 

没有改变过行高地简单赋过值cell的默认的高度是45 point ,有的是全占,有的是加上分隔线一起45pt

  方法3:苹果官方包括网上也有提到说使用下列的属性

只不过我试过了之后没有起到效果:也可能是哪些地方设置的有问题,如果你知道,你可以评论给我:

//    在iPhone8 plus的模拟器上 下边的这个暂时不管用
    [cell setLayoutMargins:UIEdgeInsetsZero];//(iOS8)
    
    [cell setDirectionalLayoutMargins:NSDirectionalEdgeInsetsZero];//(iOS11)
    cell.preservesSuperviewLayoutMargins = NO;//iOS8
  • 关于UITableViewCell的重用的问题:

Cell的重用确实能够提高性能,有的时候重用可能会带来额外的效果,比如说是有选中Cell的效果的时候,可能UITableView一滚动,就导致之前的选中的效果没了,这个问题有多种处理方法,那么先提供一中比较方便的办法,可能性能不是很好,就是根据在cell的重用的时候根据indexPath来使用不同的重用标识

代码:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"reuseId%zd",indexPath.row]];
    if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"reuseId%zd",indexPath.row]];
    }

以后要补充性能优化相关的内容在这里:

参考网址:

http://blog.sunnyxx.com/2015/04/15/ios-hide-grouped-tableview-header/

https://www.cnblogs.com/Zev_Fung/p/5654420.html

 

iOS交流群欢迎你的加入!

群二维码:

先写到这么多

如有问题,敬请指正;

如需转载,请注明出处,谢谢!

 

posted on 2017-12-20 23:15  ITCoderW  阅读(1252)  评论(0编辑  收藏  举报

导航