iOS开发- tableView的协议

在使用 UITableView 时,必须实现的协议主要包括以下几个

1. UITableViewDataSource 协议

这是最重要的协议,用于提供数据给 UITableView。没有这个协议,UITableView 是无法显示任何内容的。

必须实现的方法:

  • tableView:numberOfRowsInSection::返回给定 section 中的行数。

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    
  • tableView:cellForRowAtIndexPath::返回对应 indexPath 的单元格(UITableViewCell)。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    

这两个方法是 UITableViewDataSource 协议中最核心的必须实现的方法。

可选的方法:

  • tableView:titleForHeaderInSection::返回指定 section 的标题(用于表头)。

    - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    
  • tableView:titleForFo