美文网首页
UITable的编辑

UITable的编辑

作者: MakeThatChange | 来源:发表于2016-05-27 19:44 被阅读59次

    1)进入编辑状态

    //调用一个方法,让cell进入编辑状态进入编辑状态

    - (void)changeSegmentedC:(UISegmentedControl *)sender{

    [self.tableView setEditing:sender.selectedSegmentIndex animated:YES];

    return;

    }

    2)设置每个cell进入什么样的编辑状态

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == self.dataArr.count - 1) {

    return UITableViewCellEditingStyleInsert;

    }

    return UITableViewCellEditingStyleDelete;

    }

    3)设置哪些cell可以被编辑

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {

    return NO;

    }

    return YES;

    }

    4)自定义删除按钮的title

    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *str = [NSString stringWithFormat:@"册除%@",self.dataArr[indexPath.row]];

    return str;

    }

    5)松手后取消选中

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    }

    6)添加和删除cell

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    switch (editingStyle) {

    case UITableViewCellEditingStyleDelete:

    [self.dataArr removeObjectAtIndex:indexPath.row];

    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    break;

    case UITableViewCellEditingStyleInsert:{

    [self.dataArr addObject:@"学习"];

    NSIndexPath *indexP = [NSIndexPath indexPathForRow:self.dataArr.count - 1 inSection:0];

    [tableView insertRowsAtIndexPaths:@[indexP] withRowAnimation:UITableViewRowAnimationLeft];

    }

    default:

    break;

    }

    }

    相关文章

      网友评论

          本文标题:UITable的编辑

          本文链接:https://www.haomeiwen.com/subject/xjrfdttx.html