自定义高等Cell之xib


  • 1.创建一个继承自UITableViewCell的子类,比如IZGrouponCell

  • 2.创建一个xib文件(文件名建议跟cell的类名一样),比如IZGrouponCell.xib

    • 拖拽一个UITableViewCell出来
    • 修改cell的class为IZGrouponCell
    • 设置cell的重用标识
    • 往cell中添加需要用到的子控件
  • 3.在IZGrouponCell中

    • 将xib中的子控件连线到类扩展中
    • 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件上
    • 也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)
  • 4.在控制器中

    • 利用registerNib...方法注册xib文件

[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([IZGrouponCell class]) bundle:nil] forCellReuseIdentifier:ID];


    - 给cell传递模型数据

    ```objc
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 访问缓存池
    XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 设置数据(传递模型数据)
    cell.groupon = self.groupon[indexPath.row];

    return cell;
}

你可能感兴趣的:(自定义高等Cell之xib)