IOS UITableView

摘要:
自己iIsRegisterNib)5{6UINib*nib=[UINibnibWithNibName:@“WACustomTableViewCell”捆绑包:Nil];7〔tableViewregisterNib:nibforCellReuseIdentifier:CellIdentifier〕;8self.iIsRegisterNib=YES;9}10cell=[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];11cell.headerLabel.text=fontArray[indexPath.row];12cell.detailLabel.text=fontArray[indexPath.row];1314细胞。accessoryType=UITableViewCellAccessoryDisclosureIndicator;15} 16@catch17{18NSLog;19}III.tableView加载机制1.在IOS 8.3之前的版本中首次加载tableView。2.在IOS 8.3之后,首次加载tableView。执行UISectionRowData::refreshWithSection::tableViewRowData方法执行UITableViewDataSource::numberOfRowsInSection方法。

一、自定义TableViewCell

1. 创建Cocoa Touch Class文件,同时,生成xib文件。

2. 设置xib文件对象的基类

  IOS UITableView第1张

3. 拖拽控件(UIImageView、UILabel),并且设置其位置和大小。

  IOS UITableView第2张

4. 用拖拽方式创建控件属性代码,使用Alt+鼠标左键同时打开xib文件及基类*.h文件。再用Control+鼠标左键,将控件拖拽到*.h代码文件中,生成代码。以此生成控件与属性的关联。

  IOS UITableView第3张

  PS: 例如,UIImageView控件在*.h文件中生成代码。

5. 创建一个类文件(WATableController.h、WATableController.m),作为数据源及代理对象。

6. 创建一个类文件(WATableViewController.h、WATableViewController.m),作为root view。

二、tableViewCell

1. Cell 效率优化,使用缓存池,将不在屏幕上显示的Cell放入缓存池中,在使用时,去缓存池中取。

2. 加载xib文件,两种不同方式:

  • 通过loadNibNamed方法加载xib文件,这种方式是,在创建Cell时,会每次都要去读取文件。在读取硬盘文件上浪费效率。
     1 static NSString *CellIdentifier = @"CellIdentifierKey1";
     2     
     3     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     4     if (!cell)
     5     {
     6         cell = [[[NSBundle mainBundle] loadNibNamed:@"WACustomTableViewCell" owner:nil options:nil] firstOjbect];
     7      
     8     }
     9      
    10     cell.textLabel.text = fontArray[indexPath.row];
    11     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  • 通过UINib类,加载xib文件,只会去硬盘读取一个,以后,会将xib文件的nib对象,放入tableView数据源(放入内存)中,在需要时,从内存读取比硬盘效率比较高。
     1     WACustomTableViewCell *cell = nil;
     2     @try
     3     {
     4         if (!self.iIsRegisterNib)
     5         {
     6             UINib *nib = [UINib nibWithNibName:@"WACustomTableViewCell" bundle:Nil];
     7             [tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
     8             self.iIsRegisterNib = YES;
     9         }
    10         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    11         cell.headerLabel.text = fontArray[indexPath.row];
    12         cell.detailLabel.text = fontArray[indexPath.row];
    13         
    14         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    15     }
    16     @catch (NSException *exception)
    17     {
    18         NSLog(@"exception name: %@ , reason: %@", exception.name, exception.reason);
    19     }
      

三、tableView加载机制

 1. IOS 8.3 以前版本

  • 第一次加载tableView。
  1. 先执行初始化方法。
  2. 执行ViewDidLoad方法。
  3. 执行ViewWillAppear方法。
  4. 执行UISectionRowData::refreshWithSection::tableViewRowData方法。
  5. 执行UITableViewDataSource::numberOfRowsInSection方法。
  6. 执行UITableViewDataSource::heightForRowAtIndexPath方法。
  7. 执行UITableViewDataSource::cellForRowAtIndexPath方法。
  • 再次加载tableView从第四步开始执行。
  •  2. IOS 8.3 以后版本

    • 第一次加载tableView。
    1. 先执行初始化方法。
    2. 执行ViewController::ViewDidLoad方法。
    3. 执行ViewController::ViewWillAppear方法。
    4. 执行UISectionRowData::refreshWithSection::tableViewRowData方法
    5. 执行UITableViewDataSource::numberOfRowsInSection方法。
    6. 执行UITableViewDataSource::heightForRowAtIndexPath方法。
    7. 执行UITableViewDataSource::cellForRowAtIndexPath方法。
  • 再次加载tableVeiw执行ViewController::ViewWillAppear方法,不再执行Cell加载方法,即数据源方法(UITableViewDataSource)。
  •   

    四、遇到问题

    1. 创建VC,在其中实现UITableViewController功能,继承协议(UITableDataSource),问题是app运行,不能正常显示列表(tableview不显示),错误提示(*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x7fe778d113b0'

    解决方案:通过堆栈信息查找出现错误函数

    2. 列表在向上拖时,手机状态栏与列表信息重叠,即被遮档。

    决方案: 分为两步 第一在初始化窗口时,将获取屏幕大小方法bounds修改为applicationFrame,第二步重写设置状态栏样式方法 preferredStatusBarStylle。

    3. 没有对应的value-key

    setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.

    原因:在xib创建关联时,View关联类在file's owner的Custom class中设置关联的类名,而在Cell中,是在Custom View Table Cell的Custom class中,设置关联类名。

    4. 在tableView的Cell上加一个UIButton,而UIButton的selected和highLight事件被Cell截取。

    解决方案:

      1. 重写tableView中一些方法:

     1 - (BOOL)touchesShouldCancelInContentView:(UIView *)view
     2 {
     3     if ([view isKindOfClass:[UIButton class]] )
     4     {
     5         return NO;
     6     }
     7     else
     8     {
     9          [super touchesShouldCancelInContentView:view];
    10     }
    11     
    12     NSLog(@"touchesShouldCancelInContentView");
    13     
    14     return YES;
    15 }
    16 
    17 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    18 {
    19     [super touchesCancelled:touches withEvent:event];
    20 
    21     
    22     NSLog(@"touchesCancelled");
    23 }

    PS: 注意在ScrollView中也会有这个问题,解决方法是设置延迟事件触发,设置其delaysContentTouches = NO;。

    参考资料:http://www.cnblogs.com/wayne23/p/4011266.html

    免责声明:文章转载自《IOS UITableView》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

    上篇C++静态库中使用_declspec(dllexport) 不能导出函数的问题安装安装jenkens报错not a directory on the Jenkins master (but perhaps it exists on some agents)下篇

    宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

    相关文章

    iOS 杂笔-如何解决tableview显示错乱问题

    解决自定义Tableviewcell显示错乱问题 要是要解决这个问题,就需要用到UITableviewCell的prepareForReuse方法 cell被重用如何提前知道? 重写cell的prepareForReuse 官方头文件中有说明.当前已经被分配的cell如果被重用了(通常是滚动出屏幕外了),会调用cell的prepareForReuse通...

    使用NPOI导出导入导出Excel

    Excel2003 #region Excel2003 /// <summary> /// 将Excel文件中的数据读出到DataTable中(xls) /// </summary> /// <param name="file"></param> //...

    解读eXtremeComponents代码结构--转载

    原文地址:http://blog.csdn.net/lark3/article/details/1937466 大致整理了去年写的东西,罗列如下: ec是一系列提供高级显示的开源JSP定制标签,当前的包含的组件为eXtremeTable,用于以表形式显示数据。ec现在的版本是1.0.1,由Jeff Johnston开发的,网址:http://www.ex...

    用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)

    前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中。数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过python读取、正则表达式处理并写入Excel文档将大大减少人工处理的工作量。   1. 从gerrit获取原始信息,存入文本文档:    $ssh –p...

    MATLAB元胞数组

    MATLAB元胞数组 元胞数组: 元胞数组是MATLAB的一种特殊数据类型,可以将元胞数组看做一种无所不包的通用矩阵,或者叫做广义矩阵。组成元胞数组的元素可以是任何一种数据类型的常数或者常量,每一个元素也可以具有不同的尺寸和内存占用空间,每一个元素的内容也可以完全不同,所以元胞数组的元素叫做元胞(cell)。和一般的数值矩阵一样,元胞数组的内存空间也是动...

    实验3- 熟悉常用的 HBase 操作

    石家庄铁道大学信息科学与技术学院 实验报告 2018年----2019年第一学期 题目:熟悉常用的 HBase 操作 课程名称:大型数据库应用技术 班级:信1605-2班 姓名: XX学号: XXXXXXXX 指导教师: XXX 一、实验内容与完成情况:(实验具体步骤和实验截图说明) 实验...