美文网首页IOS | MACiOSiOS开发
iOS 关于tableView的总结

iOS 关于tableView的总结

作者: 小傑 | 来源:发表于2016-08-26 16:34 被阅读1403次

    0.性能优化

    测试帧数(注意:使用真机)
    xcode - produce - profile - core animation - 红色按钮
    
    1.行高一定要缓存
    2.不要动态创建子视图
          所有的子视图都预先创建
          如果不需要显示就hidden
    3.所有的子视图都添加到contentView上,不然处理左滑按钮事件,比如删除等可能会出现问题
    4.所有的子视图都必须设置背景色
    5.所有的颜色都不要使用alpha
    6.cell栅格化(将cell中的所有内容,生成一张独立的图像)
        //在屏幕滚动时,只显示图像
        cell.layer.shouldRasterize = YES;
        //栅格化,必须指定分辨率,否则默认使用 * 1,生成图像!
        cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
    7.异步绘制
        cell.layer.drawsAsynchronously = YES;
    

    1.不需要添加额外的滚动区域

        self.automaticallyAdjustsScrollViewInsets = NO;
    

    2.动态修改tableHeaderView的高度,重新设置过那个view的frame,再tableHeaderView一次

        [self.tableView beginUpdates];
        self.tableView.tableHeaderView = [[UIView alloc] init];// 关键是这句话
        [self.tableView endUpdates];
    

    3.得出第3个cell的位置

        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:2 inSection:0];
        //得出第3个cell的位置
        CGRect  popoverRect = [self.tableView convertRect:[self.tableView rectForRowAtIndexPath:indexPath] toView:[self.tableView superview]];
    

    .4. 这个能够避免cell点击和UITableView的点击事件的冲突
    注意:给cell添加手势,在手势的范围区域里,它会处理手势,而不是cell的点击事件,在范围外它会处理cell的点击事件

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        
        // 若为UITableViewCellContentView(即点击了tableViewCell),则不截获Touch事件
        if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
            return NO;
        }else
        {
            return YES;
        }
    }
    

    5.常用属性

           //1.分隔线
            _tableView.separatorColor = [UIColor redColor];
            _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            
    //        当tableview的类型为 plain的时候,header View 就会停留在最上面。
    //        
    //        当类型为 group的时候,header view 就会跟随tableview 一起滚动了。
    
            //关于头部,尾部控件
            //2.这样可以去掉多余的分隔线
            _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
            
            //3.高度
            _tableView.rowHeight = 10;
            //(每组的尾部高度)
            _tableView.sectionFooterHeight = 19;
            _tableView.sectionHeaderHeight = 10;
    
            //4.允许被选中
            _tableView.allowsSelection = YES;
    
            //5.编辑状态
            _tableView.editing = YES;
            
           //6. Tableview一直显示滚动条
             [self.tableView flashScrollIndicators];
            
            //7.设置UITableView的滚动条颜色
            self.tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
            
           //8. 要让tableView在编辑状态下能处理点击事件必须设置这个属性allowsSelectionDuringEditing!!!
            _tableView.allowsSelectionDuringEditing = YES;
            
            //9.更改索引的背景色
            _tableView.sectionIndexBackgroundColor = [UIColor clearColor];
            //10.更改索引的文字颜色
            _tableView.sectionIndexColor = [UIColor redColor];
    
           //11.滚动条滚动范围
            _tableView.scrollIndicatorInsets = _tableView.contentInset;
    

    6.这是遍历所有的indexPath

        for (NSIndexPath * index in tableView.indexPathsForVisibleRows)
        {
            tableView.indexPathsForSelectedRows   //所有选中的行数
            tableView.indexPathForSelectedRow     //单个选中的行数
        }
    
    

    7.索引点击事件

    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
    {
        [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];
        return index;
    }
    

    8.判断向上滑动还是向下滑动

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        
        //先定义一个oldOffset为tableView的初始位置
        
        if (scrollView.contentOffset.y > self.oldOffset) {//如果当前位移大于缓存位移,说明scrollView向上滑动
    
    
        }else
    
        
        self.oldOffset = scrollView.contentOffset.y;//将当前位移变成缓存位移
        
    }
    

    9.去掉UItableview headerview黏性(sticky)

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        if (scrollView == _tableView) {
            //这是每组头部的高度
            CGFloat sectionHeaderHeight = 36;
            
            if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
                scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
                scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            }
        }
    }
    

    10.去掉UItableview headerview Footer 黏性(sticky)

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        if (scrollView == self.tableView)
        {
            UITableView *tableview = (UITableView *)scrollView;
           //这50是每组头部尾部的高度
            CGFloat sectionHeaderHeight = 50;
            CGFloat sectionFooterHeight = 50;
            CGFloat offsetY = tableview.contentOffset.y;
            if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
            {
                tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
            }else if (offsetY >= sectionHeaderHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight)
            {
                tableview.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
            }else if (offsetY >= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height)
            {
                tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight), 0);
            }
        }
    }
    

    11.根据cell的子控件,得到对应的NSIndexPath

    //获取到对应的NSIndexPath,textField为cell的子控件
        CGPoint point = [self.textField.superview convertPoint:self.textField.frame.origin toView:self.tableView];
        NSIndexPath *index = [self.tableView indexPathForRowAtPoint:point];
    

    12.判断控件是不是cell的子控件

    if ([[[textField.superview class] description] rangeOfString:@"UITableViewCellContentView"].location != NSNotFound)
    

    13.滚动到tableview的底部

    第一种
    [self.tableView setContentOffset:CGPointMake(0 , 50 * (array.count + 2) - (ScreenHeight - NavBarHeight)) animated:YES];
    第二种
    [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    第三种
    if (self.tableView.contentSize.height <= self.tableView.frame.size.height) {
            self.tableView.contentOffset = CGPointMake(0, 0);
        } else {
            self.tableView.contentOffset = CGPointMake(0, self.tableView.contentSize.height - self.tableView.frame.size.height);
        }
    

    14.给cell添加悬浮效果(在自定义cell的时候在cell上面添加view)

    //创建一个UIView比cell.contentView小一圈
        UIView *view  = [[UIView alloc] initWithFrame:CGRectMake(10, 5, [UIScreen mainScreen].bounds.size.width - 20, 90)];//这里cell的高度为100
        view.backgroundColor = [UIColor whiteColor];
        
        //给view边框设置阴影
        view.layer.shadowOffset = CGSizeMake(1,1);
        view.layer.shadowOpacity = 0.3;
        view.layer.shadowColor = [UIColor lightGrayColor].CGColor;
        [cell.contentView addSubview:view];
    

    15.给cell左右上镂空(悬浮效果)

    1.给cell.contentView的添加UIImageView,设置UIImageView的image,highlightedImage为了让有同样的点击效果highlightedImage 的图片颜色为 [UIColor colorWithWhite:0.85 alpha:1.0]
    
    2.子控件添加到UIImageView上
    
    3.设置cell的selectedBackgroundView,backgroundColor和cell.contentView.backgroundColor为tableview的背景色即可
    
    //第二种方式,算高度的时候还是要把那20加上
    - (void)setFrame:(CGRect)frame
    {
        CGRect rect = frame;
        
        rect.origin.x += 15;
        rect.size.width -= 30;
        rect.origin.y += 20;
        rect.size.height -= 20;
        frame = rect;
        [super setFrame:frame];
    }
    

    相关文章

      网友评论

        本文标题:iOS 关于tableView的总结

        本文链接:https://www.haomeiwen.com/subject/hmdzsttx.html