UITableView详解(2)

承接上文,再续本文

一,首先我们对上次的代码进行改进,需要知道的一点是,滚动视图的时候,我们要创建几个视图,如果一个视图显示一个图片占据整个屏幕,对于滚动视图我们只需要创建三个视图就可以显示几千给图片,这就是重用机制,具体的请自觉查询,一下是重用单元格的写法,共分5部分.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //唯一标识符
    static NSString *identifier=@"cell";
    //重用单元格
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    //如果cell为空,则创建cell对象
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //为cell中的视图赋值,用于显示,这里可以设置任何值,设置只设置一个背景色
    if (indexPath.section==0) {
        cell.textLabel.text=self.aa[indexPath.row];
    }
    else if(indexPath.section==1)cell.textLabel.text=self.bb[indexPath.row];
    cell.backgroundColor=[UIColor whiteColor];
    return cell;
}

二,一个比较重要的编辑(删除增加..)方法

如果向左滑动单元格,此时没有任何效果,如果引入代理类中的方法

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  就会出现向左滑动,出现删除按钮,根据editingStyle可以判断此按钮是什么按钮,写下这个方法之后就会出现效果,即使是方法体中什么都没写!但是,效果是有了,但是点击删除按钮,其实是删除不掉任何东西的,因为还没写删除相关的代码.

 

原文地址:https://www.cnblogs.com/huntaiji/p/3448535.html