美文网首页
关于和UITableView的注意点

关于和UITableView的注意点

作者: 赖熊 | 来源:发表于2016-08-02 20:58 被阅读0次

    UITableView是我们最常用的视图,在做这个东西的时候,我遇到的几种情况:
    1,我们往往写不确定cell高度的情况下,使用高度自适应

     _tableView.estimatedRowHeight =44;
     _tableView.rowHeight =UITableViewAutomaticDimension;
    
    

    2,当底部显示的不完全的时候,我们可以设置tableview的视图的减少一部分

     _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 45, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)-64];
    

    3,cell重用的机制,就是为了每次刷新或者下拉的时候重新加载的时候不需要进行二次实例化,可以大大的减少内存的开销

     static NSString *identifer =@"cell";
        DrugStoreTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell =[[DrugStoreTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        
        [cell setInfo:self.arrlist[indexPath.row]];
    

    4,当我们写代理的时候,除了我们功能需要的代理实现之外,不必要的代理不需要实现,减少内存的开销
    5,对于入门级的初学者,对于自定义的cell的加载比较迷茫:
    两种cell的加载方式:
    1,纯代码的加载:

    static NSString *identier =@"identier";
       
    
       InfomationTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identier];
       
       if (!cell) {
           cell =[[InfomationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identier];
           
       }
    

    2,xib方式的加载:

        [self.tableView registerNib:[UINib nibWithNibName:@"InvoiceCell" bundle:nil] forCellReuseIdentifier:@"InvoiceCell"];
    
    
        InvoiceCell *cell =(InvoiceCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    

    6,在用tableview 的时候我们难免会用到UINavigationController,对于它只要理解了它的图层关系,一切都不会太难,今天就简单的讲一下关于它的用法;
    对于navigationItem其实它可以看作是navigationBar的展现形式,navigationBar包含的left,right,backBarButtonItem等展现就是靠navigationItem,所以我们每次添加左右按钮的时候都是直接用navigationItem
    我把中间的title的显示自定义了

        self.navigationItem.titleView =self.titleView;
        self.edgesForExtendedLayout =UIRectEdgeNone;//视图遮盖问题
    
    
    

    7,对于数据的加载,我们会直接用类封装,然后直接赋值给cell,对于数据的封装我们会用到

    - (instancetype)initWithInvoice:(NSDictionary *)dect
    {
        self=[super init];
        if (self) {
    //这样写的好处可以不用一个个的进行复制,这个可以快速的赋值
    
        [self setValuesForKeysWithDictionary:InfoDIC];
    //而不用这样一个个的赋值,如果数据多的话,影响开发效率而且也容易出错
    
            self.invoiceType =dect[@"invoiceType"];
            self.invoiceContent=dect[@"invoiceContent"];
            self.invoiceName =dect[@"invoiceName"];
            self.companyName =dect[@"invoiceHeadTitle"];
            self.invoiceContentId =dect[@"invoiceContentId"];
            self.invoiceHead =dect[@"invoiceHead"];
    
            self.switchType = [dect[@"isSlect"] boolValue];
        }
    
        return self;
    }
    
    
    

    第一次写随笔,只是按自己平时的想法写的,如果有什么不足欢迎指正,往后会单独写一篇关于UINavigationController的学习心得,减少别人和我一样走弯路,希望对你们有帮组

    相关文章

      网友评论

          本文标题:关于和UITableView的注意点

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