TableView
✢ Extend class UITableViewDelegate, UITableViewDataSource in UIViewController
Swift
class myViewController: UIViewcontroller { .... } extension myViewController: UITableViewDelegate, UITableViewDataSource { //Set table rows func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { .... } //Draw table cells func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { .... } .... }Objective-C
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> .... @end @implementation ViewController //Set table rows - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { .... } //Draw table cells - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { .... } .... @endReused Table Cell
- Define the UI style of the cells. (Class MyCell)
- Define every cell as MyCell.
Get data & fill data into every cell.
重複使用Cell會節省memory,理由是:比如資料總共有10筆,畫面一次顯示5筆,則被cache住的memory就只會有5筆,就算是經過scroll出現新的cell,也只不過是更新顯示的cell內容,仍然只佔用5個cell的memory。