Objective-C 学习记录 - 26

1.tableview修改和刷新数据的方法

数据的修改应该修改模型中的数据,对于tableview只需要刷新数据即可

全局刷新:

[self.tableview reloadData];  //这个方法会重载全部的cell的数据

局部刷新:

NSArray *indexPaths = @[[NSIndexPath indexPathForRow:0 inSection:0]];
[self.phoneTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];   //这个方法会根据传递的数组中的行数信息,刷新指定的行数,但这个方法的使用前提是保证数组个数不变,,如果有数据的增加或删除则要使用下面的方法

NSArray *indexPaths = @[[NSIndexPath indexPathForRow:0 inSection:0]];
[self.phoneTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];  //这个方法会根据传递的数组中的行数信息,增加行数到指定位置

NSArray *indexPaths = @[[NSIndexPath indexPathForRow:0 inSection:0]];
[self.phoneTableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationBottom];  //这个方法会根据传递的数组中的行数信息,删除指定的行数

2.设置左滑删除或其它功能
单个按钮(以删除功能为例):

/** 只要实现这个方法,就会提供系统默认的左滑删除按钮功能 */
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.phoneModels removeObjectAtIndex:indexPath.row];
    [self.phoneTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}

/** 这个方法可以修改系统提供的默认左滑按钮的文字 */
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @“删除”;
}

多个按钮:

/** 这个方法可以添加并自定义需要的左滑按钮,设置这个方法则以上两个方法会失效 */
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@“取消” handler:^
                                          (UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath){
        self.phoneTableView.editing = NO;  //退出编辑模式
    }];

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@“删除” handler:^
        (UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath){
            [self.phoneModels removeObjectAtIndex:indexPath.row];
            [self.phoneTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        }];
    
    return @[deleteAction];
}

3.UITableView的编辑模式

/** 设置一个按钮方法,点击后即可切换tableView的编辑模式 */
-(IBAction)editingMode:(id)sender
{
    //self.phoneTableView.editing = !self.phoneTableView.isEditing;
    [self.phoneTableView setEditing:!self.phoneTableView.isEditing animated:YES];
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值