04选定_取消选定_重用机制_除旧创新_使用旧控件_自定义Cell_拖拽Cell

大纲

一、自我测试
两个方法:1.选定 2.取消选定
二、重用TableViewCell
2.1 重用
重用机制:
1.若界面能显示n个Cell,系统会创建n+1个。
2.系统会将划出屏幕的Cell放入重用队列。
3.当新Cell将划入屏幕,系统会先到重用队列中寻找可重用的Cell,若无,就重新创建。
注意:为何要声明重用标识符?若单元格有多种样式,就要通过标识符进行标记。
2.2 除旧创新
找到旧控件→移除→创建新控件
2.3 使用旧控件
找到旧控件→直接使用
2.4 自定义Cell
设置属性,重写初始化方法
1.创建一个类,继承于UITableViewCell
2.在cell.contentView上添加自己想要的控件
3.声明属性,将创建的控件赋值给属性
2.5 拖拽Cell

正文
一、作业
两个方法:1.选定 2.取消选定
项目:TableView_IphoneSetting_Teacher0405

//单元格被选定
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewController1 *vc1 = [[ViewController1 alloc]init];
    NSLog(@"选中:(%d,%d)",indexPath.section,indexPath.row);
    [self.navigationController pushViewController:vc1 animated:YES];
    [vc1 release];
}
//单元格被取消选定
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"取消选中:(%d,%d)",indexPath.section,indexPath.row);
}

二、TableViewCell
内部构造:

2.1 重用
重用机制:
1.若界面能显示n个Cell,系统会创建n+1个。
2.系统会将划出屏幕的Cell放入重用队列。
3.当新Cell将划入屏幕,系统会先到重用队列中寻找可重用的Cell,若无,就重新创建。
注意:为何要声明重用标识符?若单元格有多种样式,就要通过标识符进行标记。

项目:TableViewReuseID0405
源码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.重用标识符
    static NSString *cellIdentifier = @"Cell";
    //2.(从重用队列 随机)找可重用的单元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //若无重用cell,就创建一个
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }
    cell.textLabel.text = [_array objectAtIndex:indexPath.row];
    return cell;
}

2.2 除旧创新
找到旧控件→移除→创建新控件
项目:TableView_CostumCell0405
源码:

    //移除旧图片
    UIImageView *oldImgView = (UIImageView *)[cell.contentView viewWithTag:100];
    [oldImgView removeFromSuperview];
    //创建新图片
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(240, 10, 60, 60)];
    imgView.image = [UIImage imageNamed:[_imgNameArr objectAtIndex:indexPath.row]];
    imgView.alpha = 0.5;
    imgView.tag = 100;
    [cell.contentView addSubview:imgView];
    [imgView release];

2.3 使用旧控件
找到旧控件→直接使用
项目:TableView_CostumCell2_Reuse_0405
源码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        //创建新图片
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(240, 10, 60, 60)];
        imgView.tag = 100;
        [cell.contentView addSubview:imgView];
    }
    //找到旧图片
    UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:100];
    imgView.image = [UIImage imageNamed:[_imgNameArr objectAtIndex:indexPath.row]];
    return cell;
}

2.4 自定义Cell
设置属性,重写初始化方法
1.创建一个类,继承于UITableViewCell
2.在cell.contentView上添加自己想要的控件
3.声明属性,将创建的控件赋值给属性
项目:TableView_CostumCell_CostumClass0405
源码:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        //1.创建一个类,继承于UITableViewCell
        //2.在cell.contentView上添加自己想要的控件
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(240, 10, 60, 60)];
        [self.contentView addSubview:imgView];
        //3.声明属性,将创建的控件赋值给属性
        self.customImageView = imgView;
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 40)];
        [self.contentView addSubview:label];
        self.customLabel = label;
    }
    return self;
}

2.5 拖拽Cell
项目:TableView_CustomCell_Drag0405

1.拖拽多个UITableViewCell如何获取
自我测试:
1.美团
2.一个tableView中有两种TableViewCell。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容