iOS 初学详细之 UITableView

本文详细介绍UITableView的编辑功能实现,包括创建工具栏与编辑按钮、设置编辑状态、实现删除与插入功能等关键步骤。同时,还介绍了如何通过代理方法实现单元格的编辑、删除确认提示、数据移动等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UITableView

概念:表格视图,生活中:表格由行、列组成,但是在iOS中,表格只由行来组成cell,本身也是容器。

         作用,展示数据,可以对展示的数据进行增删改查操作。tableview的大部分操作都是在代理方法中进行的.

实现编辑功能步骤:

1.创建工具栏,添加编辑按钮

//创建一个工具栏

    UIToolbar *tb=[[UIToolbar alloc] initWithFrame:CGRectMake(9, 20, 320, 30)];

    

    //创建一个编辑按钮,调用自身的编辑按钮

    UIBarButtonItem *bi=self.editButtonItem;

    

    //创建一个刷新按钮

    UIBarButtonItem *btnFlush=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(on_Flush:)];

    

    tb.items=[NSArray arrayWithObjects:bi,btnFlush, nil];

    

    [self.view addSubview:tb];

2.按钮进入编辑状态

- (void)setEditing:(BOOL)editing animated:(BOOL)animated

{

    [super setEditing:editing animated:YES];

    [tv setEditing:editing animated:YES];

}


3.UITableView进入编辑状态

-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.section) {

        return UITableViewCellEditingStyleDelete;

    }

    else

    {

        return UITableViewCellEditingStyleInsert;

    }

}


4.删除提示

-(NSString *) tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return @"确定要删除吗?";

}


5.完成具体的编辑操作

-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (editingStyle==UITableViewCellEditingStyleDelete) {

        

        //删除数据源里的数据

        [dataAry[indexPath.section] removeObjectAtIndex:indexPath.row];

        

        //删除对应的cell

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath ] withRowAnimation:UITableViewRowAnimationBottom];

    }

    else if (editingStyle==UITableViewCellEditingStyleInsert){

        

        //创建学生对象

        qfStudent *student=[[qfStudent alloc] init];

        student.stuName=@"王也";

        student.stuId=@"1033";

        

        //把学生对象加入数组中

        [dataAry[indexPath.section] addObject:student];

        

        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];

    }

}


移动效果

qfStudent * student=dataAry[sourceIndexPath.section][sourceIndexPath.row];

    

    NSMutableArray *t1Ary=dataAry[sourceIndexPath.section];

    NSInteger i=sourceIndexPath.row;

    [t1Ary removeObjectAtIndex:i];

    

    NSInteger j=destinationIndexPath.row;

    [t1Ary insertObject:student atIndex:j];




//UITableViewCell 视图,tableView中每一行都是一个UITableViewCell对象

//indexPath (section,row,用于描述此行数据位于第几分区,第几行)

//UITableViewCell 对象,被赋好值后,返回给tableView

/*UITableViewCell的重用机制

 *1、不是有多少行数据,就创建多少个cell,每个UITableView都有一个可重用的队列,滑出屏幕的cell,会被先放到可重用队列中

 *2、程序最多创建一屏+1cell 对象

 *3UITableView的重用机制,最大限度的节省了程序的内存开销,提高了程序的运行效率,重用机制对于程序的开发具有非凡的借鉴意义

 */



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

{

    //cell的可重用标识符

    static NSString *cellId = @"cell";

    //根据cell的可重用标识符,到tableView的可重用队列中,获取cell对象

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    if (cell == nil) {

        //拿不到,则alloc init

        //初始化cell,并设置cell的样式,给cell赋值可重用标识符

        //UITableViewCellStyleSubtitle 主标题,副标题样式

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId] autorelease];


    //设置cell选中的风格样式

    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    //设置cell右边的附属按钮样式

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//blue

    }

    //cell的赋值操作,写在cell == nil之外

    //根据分区下标从_data中获取array

    NSArray *array = [_data objectAtIndex:indexPath.section];

    //根据行的下标,取到array中字符串

    NSString *str = [array objectAtIndex:indexPath.row];

    //设置cell的主标题

    cell.textLabel.text = str;

    //设置cell的副标题

    cell.detailTextLabel.text = @"副标题";

    //设置头像

    cell.imageView.image = [UIImage imageNamed:@"0.png"];

    return cell;

}


//每个分区里的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;



上面的2个是必须实现的。下面的都是可选的,根据项目需求实现。



//分区数量,默认1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;


//分区的头标题和脚标题

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

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;


//行高(默认44),分区头标题高度,脚标题高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;


//将一个view设成头标题

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;


//选中表格视图某一行后,触发的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    //行被选中后,自动变回反选状态的方法

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}


- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

//必须有蓝色的按钮出现,才能点击执行这个方法,点出附属按钮

    NSLog(@“点击了第%d分区%d",indexPath.section,indexPath.row);

}




#pragma mark - editing 下面都是编辑相关


- (void)customEditMethod

{//自定义按钮实现编辑方法时,需要设置一个bool型成员变量

    _isEditing = !_isEditing;

    [_tableView setEditing:_isEditing animated:YES];

}


self.navigationItem.leftBarButtonItem = self.editButtonItem;

//作用系统自带的编辑按钮时必须按照下面的方式重写这个函数,固定写法

-(void)setEditing:(BOOL)editing animated:(BOOL)animated

{

    [super setEditing:editing animated:animated];

    [myTableView setEditing:editing animated:animated];

}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{//编辑的方法,如果其它的编辑都不设,只设了这一个,那么就会有cell右滑出现删除的按钮,也是比较常用的

    if (editingStyle == UITableViewCellEditingStyleDelete){

        //删除

        [[dataArr objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];  //删除数据源记录

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];//删除cell

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {

        //插入

        [[dataArr objectAtIndex:indexPath.section] insertObject:@"a new cell" atIndex:indexPath.row];

        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }

}


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{//删除按钮的字

    return @"";

}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{//返回编辑模式,默认是删除

    if (indexPath.section) {

        return UITableViewCellEditingStyleInsert;

    }

    return UITableViewCellEditingStyleDelete;

}


- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{//移动,不实现这个代理方法,就不会出现移动的按钮

    NSString *text=[[[dataArr objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row] retain];

    [[dataArr objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];

    [[dataArr objectAtIndex:destinationIndexPath.section] insertObject:text atIndex:destinationIndexPath.row];

    [text release];

}




- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{//默认所有的都是可以编辑的(增,删)

    return YES;

}


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{//默认所有的都是可以移动的

    return YES;

}



内容概要:本文深入解析了扣子COZE AI编程及其详细应用代码案例,旨在帮助读者理解新一代低门槛智能体开发范式。文章从五个维度展开:关键概念、核心技巧、典型应用场景、详细代码案例分析以及未来发展趋势。首先介绍了扣子COZE的核心概念,如Bot、Workflow、Plugin、Memory和Knowledge。接着分享了意图识别、函数调用链、动态Prompt、渐进式发布及监控可观测等核心技巧。然后列举了企业内部智能客服、电商导购助手、教育领域AI助教和金融行业合规质检等应用场景。最后,通过构建“会议纪要智能助手”的详细代码案例,展示了从需求描述、技术方案、Workflow节点拆解到调试与上线的全过程,并展望了多智能体协作、本地私有部署、Agent2Agent协议、边缘计算插件和实时RAG等未来发展方向。; 适合人群:对AI编程感兴趣的开发者,尤其是希望快速落地AI产品的技术人员。; 使用场景及目标:①学习如何使用扣子COZE构建生产级智能体;②掌握智能体实例、自动化流程、扩展能力和知识库的使用方法;③通过实际案例理解如何实现会议纪要智能助手的功能,包括触发器设置、下载节点、LLM节点Prompt设计、Code节点处理和邮件节点配置。; 阅读建议:本文不仅提供了理论知识,还包含了详细的代码案例,建议读者结合实际业务需求进行实践,逐步掌握扣子COZE的各项功能,并关注其未来的发展趋势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值