- DataSource: 为TableView提供需要展示的数据
- Delegate:响应用户指令,告诉这时TabelView应该做什么
- 都是使用protocol的delegate pattern
- 右击TableView, 拖动
delegate
到viewController
- ViewController是TableView的
delegate
property
-
同样的方式将
dataSource
拽到ViewController -
为新的ViewController建一个class (Cocoa Touch Class)
-
在utilities panel中,把新的ViewController屏幕和Controller Class通过Custom Class属性连接在一起
-
ViewController Class中,让这个class使用
UITabelViewDataSource
和UITableViewDelegate
的protocol
class newViewController: UIViewController, UITabelViewDataSource, UITabelViewDelegate{
//UITabelViewDataSource
//UITabelViewDelegate
}
- 把protocol中的non-optional方法添加到newViewController class中
- 这样有一个问题:为了render 100个cell,我们需要create100个objects,耗费资源
- 更好的解决方法是运用recycle的理念,在屏幕滑动时,循环使用上面不再可见的object放在屏幕最下方,这样同时只有一定数量的object被create,不受cell数量的影响
- 使用recycle cell
- 让prototype cell=1
- 给prototype cell一个identifier以供使用
- 更改tableView中cell的创建方法