ios学习记录 day34 UI10+ UITableView编辑

需要手动添加删除 移动

一.编辑过程

1.让tableView处于编辑状态

2.指定tableView哪些行可以编辑

3.指定tableView编辑样式(添加,删除)

4.编辑完成(先操作数据源,在修改UI界面)

二.移动Move

//移动的两个协议
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//上下移动的时候走 需要实现改变数据源
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSDictionary *dic = [_tableArr objectAtIndex:sourceIndexPath.section];
    NSMutableArray *arr = [dic objectForKey:@"stu"];
    
    //移动效果 不是交换
    //remove引用计数减1,arr没了就 所以要先retain +1
    Model *temp = [[arr objectAtIndex:sourceIndexPath.row] retain];
    [arr removeObjectAtIndex:sourceIndexPath.row];
    [arr insertObject:temp atIndex:destinationIndexPath.row];
    [temp release];
    
    //数据源修改之后必须刷新界面
    [tableView reloadData];
}

//不能跨区移动的协议
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;//如果section相同 移动
    }
    return sourceIndexPath;//返回原来的位置
}

 

 

//刷新所有的TableView的协议(编辑后刷新界面!)
        [tableView reloadData];

posted @ 2014-04-17 19:56  lxl奋小斗  阅读(163)  评论(0编辑  收藏  举报