美文网首页
UITableView的复用机制

UITableView的复用机制

作者: 面包与世界 | 来源:发表于2016-09-23 11:56 被阅读0次

谈到复用,首先需要了解两个代理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。

相关文章

网友评论

      本文标题:UITableView的复用机制

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