IOS开发UI进阶之UITableView三

来源:互联网 发布:凡科怎么绑定域名 编辑:程序博客网 时间:2024/05/14 04:33

一 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
原创粉丝点击