自定义UITableViewCell(1)

来源:互联网 发布:linux c语言utf8转gbk 编辑:程序博客网 时间:2024/06/06 07:02
1. ⾃定义cell就是/创建⼀个UITableViewCell的⼦类。 把cell上的控件创建都封装在⼦类中,简化UIViewController中的代码, ⼦视图控件添加到cell的contentView上.

2. 一定要将⼦视图控件添加到cell的contentView上, 这样我们在显示cell 自带的删除功能时, 可以自动将contentView向中间缩放, 而不造成形变.

3. 自定义cell的通信:
cell中声明⼀个Model类型的属性,viewController中获取到Model对象后赋值给cell的Model属性,cell中重写Model的setter⽅法,把Model对象中的内容重新赋值给各个控件, M和V不直接进⾏通信, C负责M和V之间进⾏通信.

4. 通常我们会在tableView:cellForRowAtIndexPath:⽅法中根据不同的 Model来决定使⽤什么类型的cell 每种类型的cell要定义不同的重⽤标识符 cell重⽤的时候会根据重⽤标识从重⽤队列中取⽤哪种类型的cell 


5. 实现自定义的cell#pragma mark - 制造假数据- (void)creatData {    // 创建保存假数据的数组    self.dataArr = [NSMutableArray array];    for (int i = 0; i < 10; i ++) {        CellModel *cellModel = [[CellModel alloc] init];        cellModel.name = [NSString stringWithFormat:@"名字:%d", i];        cellModel.gender = [NSString stringWithFormat:@"性别:%d", i];        cellModel.imageName = @"11";                // 在model中设置isSelected属性, 默认所有的cell未点击        cellModel.isSelected = NO;        [self.dataArr addObject:cellModel];        [cellModel release];    }}#pragma mark - 创建tableView- (void)creatTableView {    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];    self.tableView.backgroundColor = [UIColor orangeColor];    self.tableView.dataSource = self;    self.tableView.delegate = self;    [self.view addSubview:self.tableView];    [self.tableView release];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *identifer = @"myCell";    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];    if (cell == nil) {        cell = [[[MyTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifer] autorelease];    }    /* 这里实现点击一个cell 就更改cell的image     最初的实现:      // 取出对应的cell    MyTableViewCell *cell = (MyTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];     // 直接修改image      cell.myImageView.image = [UIImage imageNamed:@"12"];     思考重点:     1. 换图片后出现了什么问题?     当cell从屏幕中划出后, 再次出现在屏幕中, 图片又变回原来的图片了.     2. 为什么会出现这种情况?     当cell从屏幕中滑出, 进入复用集合, 再显示时从集合中取出, 取出后重新将之前设置的值赋值, 所以点击图片失效.(重用的问题)     3. 如何解决     1>方法一: 更改数据源中的model值;     2>方法二: 加标签, 在model上加标签, 可以设置成枚举, 实现多种要求, 拓展性高    */    CellModel *model = self.dataArr[indexPath.row];       //2>方法二的实现: 加标签, 在model上加标签(isSelected)    // model里加一个是否点击的标签    // 如果点击, 标签就是yes, yes时就显示yes 的图片    // 没有点击就是no, 就用no 的图片    if (model.isSelected == NO) {        cell.myImageView.image = [UIImage imageNamed:model.imageName];    } else if (model.isSelected == YES) {        cell.myImageView.image = [UIImage imageNamed:@"12"];    }    // 给cell赋值    cell.myTextLabel.text = model.name;    cell.myDetalLabel.text = model.gender;    // 这里有另外一种给cell赋值的方法,     [cell cellForModel:model];    return cell;}

这里有另外一种给model赋值的方法: 虽然在View中赋值打破了MVC模式, 但是体现了很好的封装性, @implementation MyTableViewCell#pragma mark 用来接收model的方法- (void)cellForModel:(CellModel *)model {    NSLog(@"%@", model.name);    // 拿到model可以直接进行赋值    self.myTextLabel.text = model.name;    self.myDetalLabel.text = model.gender;    self.myImageView.image = [UIImage imageNamed:model.imageName];}@end

#pragma mark - 点击cell更换图片- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    // 取出model更改标签状态    CellModel *model = self.dataArr[indexPath.row];    model.isSelected = !model.isSelected;      // 取出对应的cell    MyTableViewCell *cell = (MyTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];    // 根据标签更改需要的图片    if (model.isSelected == YES) {        cell.myImageView.image = [UIImage imageNamed:@"12"];    } else        cell.myImageView.image = [UIImage imageNamed:@"11"];      [tableView reloadData];       // 1>方法一在点击中的实现://    CellModel *model = self.dataArr[indexPath.row];//    model.imageName = @"12";}



0 0