TableView 插入表格和删除表格

来源:互联网 发布:淘宝店铺怎么改折扣价 编辑:程序博客网 时间:2024/05/22 01:43

@property NSMutableArray *objects;


//插入数据在第0行

- (void)insertNewObject:(id)sender {

    if (!self.objects) {

        self.objects = [[NSMutableArrayalloc] init];

    }

    [self.objectsinsertObject:[NSDatedate] atIndex:0];

    NSIndexPath *indexPath = [NSIndexPathindexPathForRow:0inSection:0];

    [self.tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

}

//设置可编辑状态

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

    // Return NO if you do not want the specified item to be editable.

    return YES;

}

//删除所编辑的单元格

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

    if (editingStyle ==UITableViewCellEditingStyleDelete) {

        [self.objectsremoveObjectAtIndex:indexPath.row];

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

    } elseif (editingStyle ==UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.

    }

}



0 0
原创粉丝点击