zoukankan      html  css  js  c++  java
  • UITableView 删除cell

    滑动删除、通过一个按钮控制删除、替换滑动出的delete

    UITableVeiw Delete

    1. 直接滑动删除
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }
     
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
     
         if (editingStyle == UITableViewCellEditingStyleDelete) {
             [dataArray removeObjectAtIndex:indexPath.row];
             // Delete the row from the data source.
             [testTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
             
         }   
         else if (editingStyle == UITableViewCellEditingStyleInsert) {
         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
         }   
     }

    2.通过一个按钮控制是否能删除  设置能删除 [tableVeiw setEditing:YES animated:YES];

    //要求委托方的编辑风格在表视图的一个特定的位置。 //当在Cell上滑动时会调用此函数
    -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;//默认没有编辑风格
        if ([tableView isEqual:myTableView]) {
            result = UITableViewCellEditingStyleDelete;//设置编辑风格为删除风格
        }
        return result;
    }

    -(void)setEditing:(BOOL)editing animated:(BOOL)animated{//设置是否显示一个可编辑视图的视图控制器。
        [super setEditing:editing animated:animated];
        [self.myTableView setEditing:editing animated:animated];//切换接收者的进入和退出编辑模式。
    }

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//请求数据源提交的插入或删除指定行接收者。
        if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式
            if (indexPath.row<[self.arrayOfRows count]) {
                [self.arrayOfRows removeObjectAtIndex:indexPath.row];//移除数据源的数据
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//移除tableView中的数据
            }
        }
    }
    3.替换滑动出的delete (可以把delete替换为汉字 或别的字母)

    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return @"删除"; //或return @"delete a cell"; //如果只要求设置为汉字也可以不用这个 直接在设置中设置只支持中文
    }

  • 相关阅读:
    java wait方法
    thread join
    AtomicReference 原理
    exchanger java另一种栅栏
    CyclicBarrier 栅栏 原理,应用场景
    信号量Semaphore
    FutureTask 解析
    CountDownLatch
    java 双端队列 Deque
    lockInterruptibly 和lock 原理
  • 原文地址:https://www.cnblogs.com/feng-a/p/4211339.html
Copyright © 2011-2022 走看看