加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

UI类 - UITableView增删移

(2013-05-21 18:21:19)
标签:

uitableview怎删移

it

分类: Mac/IOS那些事
// Override to support conditional editing of the table view.
//定义单元格可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row ==0) {
        return  NO;
    }
    return YES;
}
//定义单元个编辑的类型 是删除还是添加
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row ==0) {
        return  UITableViewCellEditingStyleNone;
    }else if (indexPath.row==1)
    {
        return UITableViewCellEditingStyleInsert;
    }
    return UITableViewCellEditingStyleDelete;
}


// Override to support editing the table view.
// 实现删除和添加
static int insertCount=1;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_ListArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
     
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        NSString *insertContent = [NSString stringWithFormat:@"new Font,%d",insertCount];
        [_ListArray insertObject:insertContent atIndex:indexPath.row+1];
        NSIndexPath *_indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[_indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        insertCount ++;
     
}

// Override to support conditional rearranging of the table view.
//定义可否移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}

// Override to support rearranging the table view.
//实现移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    NSString *moveTxt = [[_ListArray objectAtIndex:fromIndexPath.row]retain];
    [_ListArray removeObject:moveTxt];  //注意这里需要 retain 或者copy一下
    [_ListArray insertObject:moveTxt atIndex:toIndexPath.row];
    [moveTxt release];

}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有