UITableView的复用机制

谈到复用,首先需要了解两个代理UITableViewDelegateUITableViewDataSource。四个方法:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

这几个方法分别设置了一组中cell的个数、cell的内容、cell的高、选中cell的结果。
只有在cell被滑动出界面的时候,此cell才会被加入到复用队列中。每次在创建cell的时候,程序会首先通过调用dequeueReusableCellWithIdentifier("cellType")方法,到复用队列中去寻找标示符为“cellType”的cell,如果找不到,返回nil,然后程序去通过调用

    cell = UITableViewCell.init(style: .Subtitle, reuseIdentifier: "cellType")

来创建标示符为“cellType”的cell。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容