iOS UITableView中刷新和編輯操作

UITableView的刷新和編輯操作


1.刷新操作

  • 全局刷新
    • 先修改模型數據
    • 然后刷新界面
    AZWine *wine=self.winesArray[0];
    wine.name=@"xoxo";
    wine.money=@"100";
   
    [self.tableView reloadData];
  • 局部刷新
    // 先修改模型
    AZWine *wine=self.winesArray[0];
    wine.name=@"xoxo";
    wine.money=@"100";
    AZWine *wine2=self.winesArray[3];
    wine2.money=@"55";
    
    // 確定需要修改的cell
    NSArray *array=@[[NSIndexPath indexPathForRow:0 inSection:0],[NSIndexPath indexPathForRow:3 inSection:0]];
    
    [self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationRight];

2.左滑刪除

  • 系統默認方法
/**
 *  只要實現這個方法,就擁有左滑刪除功能
 */
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.winesArray removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}

/**
 *  替換默認的左滑文字
 */
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"刪除";
}

  • 自定義左滑按鈕
/**
 *  自定義左滑按鈕
 *  如果實現了這個方法默認會執行這個方法,而不會執行上面的系統方法
 */
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 添加自定義按鈕
    UITableViewRowAction *action1=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"關注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        // 退出編輯模式
        self.tableView.editing=NO;
    }];
    
    UITableViewRowAction *action2=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
       // 刪除按鈕
        [self.winesArray removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }];
    
    // 返回自定義按鈕
    return @[action2,action1];
    
}


編輯模式

3.編輯模式下批量刪除

  • 進入編輯模式
    // 沒有動畫
    self.tableView.editing=!self.tableView.isEditing;
    
    // 有動畫 
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
    
  • 批量刪除
    • 獲得選中的模型,得注意不能遍歷數組逐一刪除,刪除操作會導致數組下標發生變化,將所有選中的模型放在同一個數組中
    • 移除選中模型
    • 移除模型對應的cell
    • 恢復未編輯狀態
 // 獲得被選中的模型(千萬不能遍歷所有元素一條一條刪除,因為索引值可能會因為被刪除的元素發生改變)
    NSMutableArray *deletedWine=[NSMutableArray array];
    for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
        [deletedWine addObject:self.winesArray[indexPath.row]];
    }
    // 移除酒模型
    [self.winesArray removeObjectsInArray:deletedWine];
    
    // 刷新cell
    [self.tableView deleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationRight];
    
    // 恢復未編輯狀態
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
    self.deleteBtn.enabled=NO;
批量刪除

4.自定義編輯刪除

  • 在模型中增加一個選中狀態屬性
/*選中狀態*/
@property(nonatomic,assign,getter=isChecked)BOOL checked;
  • AZWineCell.m的文件中添加選中的圖片
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 添加選中的控件
        UIImageView *checkedIconView=[[UIImageView alloc]init];
        checkedIconView.image=[UIImage imageNamed:@"check"];
        
        [self.contentView addSubview:checkedIconView];
        self.checkedIconView=checkedIconView;
        
    }
    return self;
    
}
  • 在layoutSubviews中確定控件的尺寸
-(void)layoutSubviews
{
    // !!一定不要忘了調用
    [super layoutSubviews];
    
    // 確定打勾控件的frame
    CGFloat checkWH=24;
    CGFloat checkedX=self.contentView.bounds.size.width-20-checkWH;
    CGFloat checkedY=(self.contentView.bounds.size.height-checkWH)*0.5;
    self.checkedIconView.frame=CGRectMake(checkedX, checkedY, checkWH, checkWH);
    
    // 確定標題的frame
    CGRect textFrame=self.textLabel.frame;
    textFrame.size.width=self.contentView.frame.size.width-  textFrame.origin.x-checkWH-20;
    self.textLabel.frame=textFrame;
    
}

  • 在AZWine的set方法中控制選中圖片的顯示與隱藏
-(void)setWine:(AZWine *)wine
{
    _wine=wine;
    
    // 給控件注入數據
    self.imageView.image=[UIImage imageNamed:wine.image];
    self.textLabel.text=wine.name;
    self.detailTextLabel.text=[NSString stringWithFormat:@"¥%@",wine.money];
    
    // 根據模型的checked屬性確定控件的顯示與隱藏
    if (wine.isChecked) {
        self.checkedIconView.hidden=NO;
    }else{
        self.checkedIconView.hidden=YES;
    }
   
}
  • 在ViewController.m文件中新建一個selectedIndexPath屬性用來記錄選中cell的indexPath
@property(nonatomic,strong)NSMutableArray *selectedIndexPath;
  • 監聽用戶點擊,切換選中狀態,進行刪除操作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取出被選中cel的模型
    AZWine *wine=self.winesArray[indexPath.row];
    
    // 設定選中的狀態
    if (wine.isChecked) {
        wine.checked=NO;
        [self.selectedIndexPath removeObject:indexPath];
    }else{
        wine.checked=YES;
        [self.selectedIndexPath addObject:indexPath];
    }
    
    // 刷新表格
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

// 批量刪除
-(IBAction)multiplyDelete
{
    // 獲得被選中的模型
    NSMutableArray *deletedWine=[NSMutableArray array];
    for (NSIndexPath *indexPath in self.selectedIndexPath) {
        [deletedWine addObject:self.winesArray[indexPath.row]];
    }
    // 移除酒模型
    [self.winesArray removeObjectsInArray:deletedWine];
    
    // 刷新cell
    [self.tableView deleteRowsAtIndexPaths:self.selectedIndexPath withRowAnimation:UITableViewRowAnimationRight];
    
    // 清空數組
    [self.selectedIndexPath removeAllObjects];
  
}

自定義批量編輯操作
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容