标签:
1.MVC
根据MVC的思想,UITableView作为View需要data source作为Model,需要view controller 和delegate作为Controller。UITableViewcontroller的实例同时满足这3个角色:view controller、data source和delegate。
2.UITableViewCell
table view的每一行都是一个UITableViewCell的实例,在Cell内部有一个UIView控件(contentView,作为其他元素的父控件),包括:两个UILable控件(textLabel、detailTextLabel)、一个UIImage控件(imageView)。这3个子控件在contentView中是否显示以及显示位置可以通过UITableViewCellStyle进行设置:UITableViewCellStyleDefault、UITableViewCellStyleSubtitle、UITableViewCellStyleValue1、UITableViewCellStyleValue2。
3. Cell的复用机制
滑动table,离开屏幕的cell放进cell池里供复用。data source每次需要新cell会先检查cell池中是否有可用的cell,否则再创建新cell。
4. 常用的UITableView函数
(1)最简单的表格显示:
tableView: cellForRowAtIndexPath: (cell.imageView.image设置图片、cell.detailTextLabel.text 追加细节标签、cell.accessoryType追加附件)
tableView:numberOfRowsInSection:
tableView:heightForRowAtIndexPath:
(2)单元选择时的动作:
tableView: didSelectRowAtIndexPath:
(3) 表格的分段显示:
numberOfSectionsInTableView:
tableView: titleForHeaderInSection:
tableView: titleForFooterInSection:
tableView:heightForHeaderInSection:
(4) 编辑(删除、追加单元、单元移动)
tableView:commitEditingStyle:forRowAtIndexPath:
tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:
tableView: editingStyleForRowAtIndexPath:
tableView:canEditRowAtIndexPath:
tableView: moveRowAtIndexPath:toIndexPath:
tableView:canMoveRowAtIndexPath:
tableView: targetIndexPathForMoveFromRowAtIndexPath: toProposedIndexPath:
5.TableView常用函数的执行顺序:
numberOfSectionsInTableView:
tableView:heightForHeaderInSection:
tableView: numberOfRowsInSection:
tableView:heightForRowAtIndexPath:
tableView:cellForRowAtIndexPath:
总结:
先section后row,
对于每个个体单元顺序:(1)number:有多少(2)height:每个的高度(3)Cell:具体值
对于tableView最重要的两个函数tableView: cellForRowAtIndexPath:和tableView:heightForRowAtIndexPath:的调用顺序:
(1)UITableView多次调用tableView:heightForRowAtIndexPath:确定它的contentSize及Cell的位置;
(2)然后调用tableView:cellForRowAtIndexPath:,把重用的Cell放置到对应的位置上。
由于heightForRowAtIndexPath:是调用次数最多的方法,在heightForRowAtIndexPath:里尽量不使用cellForRowAtIndexPath:及cell.frame.size.height得Cell的高度。总之,这两个代码各司其职,不重叠代码。
6.注意事项:
(1)如果更改了dataSource数组,而没有调用Reload,那么cellForRowAtIndexPath:时还是按照原先的dataSource数组进行渲染,程序就挂了。 [self.tableView reloadData]:UI的刷新使用了UITableView的reloadData方法,该方法会重新调用数据源方法,包括计算分组、计算每个分组的行数,生成单元格等刷新整个UITableView。
(2)dataSource为NSArray类型的,使用objectAtIndex取数据时,必须进行判断,避免超出数组的范围:
例如:if (self.dataArr.count > indexPath.section) {
[self.dataArr objectAtIndex:indexPath.section];
}
(3)关于上拉加载需要注意:
(3.1)cellForRowAtIndexPath:中,要考虑“加载Cell”的存在
if (indexPath.section == self.dataArr.count){
//显示“加载cell“
}
(3.2)numberOfSectionsInTableView:中
在需要加载cell时,要考虑“加载Cell”的存在,即在原来的dataSource数量的基础上加1;
(3.3)cellForRowAtIndexPath:和numberOfSectionsInTableView:两个函数中多种Cell情况的判断逻辑必须要全面。
比如cellForRow,最后return [UITableViewCell new];排除有可能没考虑到的情况
(4)Cell在不同手机屏幕适配问题:
(4.1)屏幕尺寸宽320和375,Cell设计时使用低分辨率(宽320),避免使用375在iphone5上显示不全。
(4.2)layOutSubView是根据View上的constraints(AutoLayOut)去决定subViews的position和size。我之前的IB中设置的constraints有很多冲突的,所以遇到layOut的代码,就会出现view错乱的情况。解决办法是暂不用autoLayOut,使用Spring设置更加直观。
代码View中已经根据坐标布局好的view,可以不用多此一举layoutSubviews。
对于代码创建的View,-(void)layoutSubviews方法最后,加上[self layoutIfNeeded];
(4.3)有些无法解释的情况,记录下来以后参考:
如下代码,在iphone5上居中,但在iphone6 Plus上图像不居中,
Img.centerX = cell.contentView.centerX;
更改代码如下,则所有手机显示OK:
Img.centerX = SCREEN_WIDTH/2;
(5)弹出的tableView的显示行数要考虑屏幕高度,不能高于屏幕高度。
任何UIKit控件的学习可以用以下类似步骤建立一个可以跑的小程序进行练习:
(1)新建Single View application;
(2)创建继承自UITableViewController的TableViewController类,
在主程序的delegate里设置:self.window.rootViewController = tableViewcontroller;
(3)在自己创建的rootViewController里使用新知识进行练习(本UITableView的Model可以用用NSArray作为dataSource代替复杂的Model或者动态的Model数据)。
标签:
原文地址:http://www.cnblogs.com/Xylophone/p/4594232.html