iOS入门(三十四) 表视图的编辑

本文详细介绍了如何在iOS应用中实现表视图的编辑和移动功能,包括初始化、编辑开关、数据源管理、代理方法重写以及移动规则设定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

表视图的编辑
表视图的移动  


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

        self.nameArr = [[NSMutableArray alloc]initWithObjects:@"龙XX",@"王XX",@"贾XX",@"李XX",@"张XX",@"郑XX",@"马XX",@"王XX",@"马XX",@"尚XX",@"姜XX",@"付XX",@"张XX",@"臧XX",@"付XX",@"裴XX",@"谭XX",@"李XX",@"王XX",@"刘XX", nil];

    }

    return self;

}

-(void)dealloc

{

    [_tableview release];

    [_nameArr release];

    [super dealloc];

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 440) style:UITableViewStylePlain];

    [self.view addSubview:_tableview];

    [_tableview release];

    _tableview.delegate self;

    _tableview.dataSource self;

    //1、设置编辑的开关

    self.navigationItem.rightBarButtonItem self.editButtonItem;

}

-(void)setupArr

{

    

}

//2、重写系统的viewController方法

-(void)setEditing:(BOOL)editing animated:(BOOL)animated

{

    [super setEditing:editing animated:animated];

    NSLog(@"++++%d",editing);

    //利用viewcontroller 的编辑状态,改变TableView的编辑状态

    [_tableview setEditing:editing animated:animated];

}

#pragma mark - tableview的代理方法

//3、根据indexPath判断cell能否被编辑

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

{

    if (indexPath.row == 12 ) {

        return  NO;

    }

    return YES;

}

//4、根据indexPath指定编辑的样式(删除/添加/多选)

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

{

    if (indexPath.row == ) {

        return UITableViewCellEditingStyleDelete;

    }

    return     UITableViewCellEditingStyleInsert;

}

//5、根据所选的行,执行不同的方法

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

{

    if (editingStyle == UITableViewCellEditingStyleDelete ) {

        //首先,删除数据源中相应的数据

        [self.nameArr removeObjectAtIndex:indexPath.row];

        //然后,更新tableView的显示

        //方式一:强制TableView重新执行所有的代理方法

//        [tableView reloadData];

        //方式二:利用TableView的删除方法,删除掉相应的cell

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

    }

    if (editingStyle == UITableViewCellEditingStyleInsert) {

        [self.nameArr insertObject:@"花花" atIndex:indexPath.row];

        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

    }

}

#pragma mark-tableView 的移动

//判断哪些行可以移动

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.row == ) {

        return NO;

    }

    return YES;

}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

    [self.nameArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

    [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];

}

-(NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

{

    NSLog(@"%@====%@",sourceIndexPath,proposedDestinationIndexPath);

    //限制cell的跨区移动

    if (sourceIndexPath .section != proposedDestinationIndexPath.section) {

        return sourceIndexPath;

    }

    return proposedDestinationIndexPath;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.nameArr.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString  * cellID = @"联系";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];

}

//    cell.imageView.image =

//    UIIma[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",indexPath.row +1]];

    cell.textLabel.text = [NSString stringWithFormat:@"%@",[_nameArr objectAtIndex:indexPath.row]];

    cell.backgroundColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.8 alpha:0.7];

//    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    

    return cell;

}





    NSString * strPath = [[NSBundle mainBundle]pathForResource:@"随便" ofType:@"plist"];

    NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile:strPath];

    NSLog(@"%@",dic);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值