Cyph3r Cyph3r - 4 months ago 36
Objective-C Question

Move tableview cell

Hi everyone i am newbie to iOS.

I have implemented a tableview with 3 sections in TableViewController.And i have displayed the section rows to each section.

- (void)viewDidLoad
{
[super viewDidLoad];

self.clearsSelectionOnViewWillAppear = NO;

self.navigationItem.rightBarButtonItem = self.editButtonItem;

sectionTittle=[[NSMutableArray alloc]initWithObjects:@"Real Madrid",@"Barcelona",@"Liverpool", nil];
firstSection=[[NSMutableArray alloc]initWithObjects:@"Cr7",@"Garath Bale",@"Pepe",
nil];
secondSection=[[NSMutableArray alloc]initWithObjects:@"Lionel Messi",@"Neymar",@"Pique",
nil];
thirdSection=[[NSMutableArray alloc]initWithObjects:@"Gerrard",@"Saurez",@"Lallana",
nil];


}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionTittle.count;
}

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


if(section==0)
{
return [firstSection count];
}
if(section==1)
{
return [secondSection count];
}
if(section==2)
{
return [thirdSection count];
}
return 0;





}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hi" forIndexPath:indexPath];


cell.textLabel.textColor=[UIColor brownColor];



if (indexPath.section == 0)
{
cell.textLabel.text=[firstSection objectAtIndex:indexPath.row];


}


if (indexPath.section == 1)
{
cell.textLabel.text = [secondSection objectAtIndex:indexPath.row];
}
if (indexPath.section == 2)
{
cell.textLabel.text = [thirdSection objectAtIndex:indexPath.row];
}




// Configure the cell...
return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

return [sectionTittle objectAtIndex:section];
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}


Now my output looks like



I want to move the rows between the these 3 sections how can i do it..? while i try it doing the row moves between the section but while saving it create a problem.

How can i go through it..?

Thanks

SHN SHN
Answer

You need to utilise UITableView re-ordering capacity. The cells can be re-ordered across sections. To enable re-ordering mode:

[self.tableView setEditing:TRUE animated:TRUE];

This enables the TableView in editing mode which includes enables deletion mode along with re-ordering. If you do not want the deletion mode implement:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;

}

The re-order support accessory view is shown if you have implemented moveRowAtIndexPath like, for your case:

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

// Logic to change data source
NSMutableArray *deleteFrom = [self getDataSourceArrayFromIndexPath:sourceIndexPath];
NSMutableArray *addTo = [self getDataSourceArrayFromIndexPath:destinationIndexPath];
[addTo insertObject:[deleteFrom objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath.row];
[deleteFrom removeObjectAtIndex:sourceIndexPath.row];

}

where getDataSourceArrayFromIndexPath is:

-(NSMutableArray*) getDataSourceArrayFromIndexPath:(NSIndexPath*) index
{
switch (index.section) {
    case 0:
        return self.firstSection;
        break;
    case 1:
        return self.secondSection;
        break;
    case 2:
        return self.thirdSection;
        break;
    default:
        break;
}
return nil;
}