IOS开发UI进阶之UITableView三

一 cell数据刷新之全局刷新

[self.tableView reloadData];
  • 添加数据
 // 1.修改模型数组数据
 Wine *newWine = [[XMGWine alloc] init];
 newWine.name = @"五粮液";
 newWine.image = @"xx";
 newWine.money = @"630";

 [self.wines insertObject:newWine atIndex:0]; //数据加到前面 
    //[self.wines addObject:newWine];  数据默认加到最后
 // 2.刷新tablView(重新调用所有的数据源方法)
 [self.tableView reloadData];
  • 删除数据
// 1.修改数据
    [self.wines removeObjectAtIndex:0];   
// 2.刷新
    [self.tableView reloadData];
  • 更新数据
 NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
  Wine *wine = self.wines[indexPath.row];

    wine.name = @"好酒";
    wine.money = @"60";

// 2.刷新
[self.tableView reloadData];

二 cell数据刷新之局部刷新

  • 刷新特定行的cell
[self.tableView reloadRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
  • 插入特定行数的cell
[self.tableView insertRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
  • 删除特定行数的cell
[self.tableView deleteRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];

  • 数据刷新的原则
    • 通过修改模型数据,来修改tableView的展示
      • 先修改模型数据
      • 再调用数据刷新方法
    • 不要直接修改cell上面子控件的属性

三 左滑删除

  • 遵守协议 – UITableViewDelegate
  • 点击了“左滑出现的Delete按钮”会调用这个方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 删除模型
    [self.wineArray removeObjectAtIndex:indexPath.row];

    // 刷新
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
  • 修改Delete按钮文字为“删除”
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
//    return [NSString stringWithFormat:@"删除-%zd", indexPath.row];
}
  • 左滑出现多个选项
-(NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction * action, NSIndexPath * indexPath) {
       NSLog(@"点击了关注");

       // 收回左滑出现的按钮(退出编辑模式)
       tableView.editing = NO;
   }];
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * action, NSIndexPath * indexPath) {
        [self.wineArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];

    return @[action1, action0]; // 决定action顺序
}

四 编辑模式和批量删除

  • tableView.editing = YES时,tableView进入编辑模式
    • 默认情况下,进入编辑模式时,左边会出现一排红色的“减号”按钮
- (IBAction)remove{
  //  self.tableView.editing = !self.tableView.isEditing;
  [self.tableView setEditing:!self.tableView.isEditing animated:YES];
  }
  • 退出编辑模式时隐藏删除按钮
self.removeButon.hidden = !self.tableView.isEditing;
  • 批量删除
- (void)viewDidLoad {
    [super viewDidLoad];
    // 编辑模式的时候可以多选
    self.tableView.allowsMultipleSelectionDuringEditing =YES; 
    // 隐藏删除按钮
    self.removeButon.hidden = YES;   
}
  • 删除选中行cell数据
 // 获得需要删除的酒模型数据
     NSMutableArray *deletedWineArray = [NSMutableArray array];
     for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows/* 选中的行号*/) {
       [deletedWineArray addObject:self.wineArray[indexPath.row]];
      }
// 删除模型数据
    [self.wineArray removeObjectsInArray:deletedWineArray];
// 刷新
   [self.tableView deleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationLeft];

五 自定义批量删除

  • 选中cell右侧显示打钩图片,再点击取消打钩
//选中当前行的时候
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Wine *wine =self.wineArray[indexPath.row];

    wine.check = !wine.isCheck;
    [self.tableView reloadData];
}
  • 千万不要直接操作cell,一定要通过模型修改(防止cell重用)
/** 是否勾选 */
@property (nonatomic, assign,getter=isCheck) BOOL *check;
  • 通过模型传递给cell
@interface XGWineCell()
@property (weak, nonatomic) IBOutlet UIImageView *checkImageView;
@end

- (void)setWine:(XGWine *)wine
{
    _wine = wine;
    .... // 设置数据
//    根据模型的checked属性决定打钩控件是显示还是隐藏
    self.checkImageView.hidden = !wine.isCheck;
 }   
  • 删除打钩cell
- (IBAction)remove
{
//    获取所有打钩的模型
    NSMutableArray *array =[NSMutableArray array];
    for (XMGWine*wine in _wineArray)
    {
        if (wine.isCheck) {
            [array addObject:wine];
        }

    }
//    删除所有打钩的模型
    [_wineArray removeObjectsInArray:array];
    [self.tableView reloadData];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值