温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

UITableViewCell两个协议 UITableViewDelegate 和 UITableViewDataSource

发布时间:2020-08-05 09:42:18 来源:网络 阅读:2062 作者:yjf123546 栏目:开发技术


一, UITableViewDataSource

1,必须实现 设置每个分区的行数

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

2,必须实现, 设置每个分区的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

3,设置分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

4,为每个分区设置标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

5,为分区设置索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

6,设置tableView每行的编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowA tIndexPath:(NSIndexPath *)indexPath

7,当提交编辑操作时触发(插入或删除)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

8,设置tableView每一个cell是否允许移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

9,提交移动操作之后触发
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

二, UITableViewDelegate


10,设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

11,设置cell选中的事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath


12,设置tableViewCell的编辑样式
设置tableViewCell的编辑样式(插入删除) - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

13,设置当点击删除按钮时提示的确认文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)

14,设置cell的移动位置
设置cell移动的位置, - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{    //现在移动时只能在本区内移动    //sourceIndexPath 原地址    //proposedDestinationIndexPath将要移动到的地址    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {        //如果是同一个分区,返回目的地址        return proposedDestinationIndexPath;    }        //如果不是同一个分区,返回原来的地址    return sourceIndexPath;    }

三,处理编辑操作详细步骤

 1,tableView 进入编辑状态 方法(-(void)setEditing:(BOOL)editing animated:(BOOL)animated)  2,设置每一行的编辑状态 方法(- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath)  3,设置每一行的编辑样式(可选) 方法(- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath)  4,处理编辑操作(插入/删除) 方法(- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath)

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI