tableview cell的移动

来源:互联网 发布:古剑奇谭ol激活码淘宝 编辑:程序博客网 时间:2024/04/30 16:44
- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.    array1= [[NSMutableArray alloc]initWithObjects:@"1",@"3", @"5",@"7", @"9",@"11", nil];    array2 =[[NSMutableArray alloc]initWithObjects:@"2",@"4", @"6",@"8", nil];    [tableview setEditing:YES animated:YES];//移动  要设置可编辑}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 2;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    if (section == 0) {        return [array1 count];    }else    {        return [array2 count];    }    return 0;}-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    if (section ==1) {        return @"更多";    }    return @"";}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell * cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];    if (indexPath.section ==0) {        cell.textLabel.text = [array1 objectAtIndex:indexPath.row];    }else if (indexPath.section ==1)    {     cell.textLabel.text = [array2 objectAtIndex:indexPath.row];    }    return cell;}#pragma mark 决定tableview的编辑模式- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {    return UITableViewCellEditingStyleNone;}-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}//移动的时候改变数据源-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    if ([sourceIndexPath section]==[destinationIndexPath section]) {        if ([sourceIndexPath section]==0) {            NSString * source =[array1 objectAtIndex:sourceIndexPath.row];            [array1 removeObjectAtIndex:sourceIndexPath.row];            [array1 insertObject:source atIndex:destinationIndexPath.row];                        }else if ([sourceIndexPath section]==1)        {            NSString * source =[array2 objectAtIndex:sourceIndexPath.row];            [array2 removeObjectAtIndex:sourceIndexPath.row];            [array2 insertObject:source atIndex:destinationIndexPath.row];        }    }else    {    NSString * source =[array1 objectAtIndex:sourceIndexPath.row];    NSString * destination =[array2 objectAtIndex:destinationIndexPath.row];    [array1 replaceObjectAtIndex:[sourceIndexPath row] withObject:destination];    [array2 replaceObjectAtIndex:[destinationIndexPath row] withObject:source];    }    NSLog(@"array1======%@\n  array2==%@",array1,array2);}

0 0
原创粉丝点击