2016-07-22 3 views
0

Привет всем Я новичок в iOS.Переместить UITableViewCell

Я внедрил UITableView с 3 разделами в UITableViewController. И я показывал строки раздела для каждого раздела.

- (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; 
    } 

Теперь мой результат выглядит следующим образом:

Я хочу, чтобы переместить строки между этими 3-х секций. Как мне это сделать..? Пока я пытаюсь сделать это, строка перемещается между секцией, но при сохранении она создает проблему.

Как я могу пройти через это??

Благодаря

ответ

0

Вам необходимо использовать возможность переустройства UITableView. Ячейки могут быть повторно упорядочены по разделам. Для включения режима повторного заказа:

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

Это позволяет TableView в режиме редактирования, который включает в себя режим позволяет удалить вместе с переупорядочиванием. Если вы не хотите, режим удаления реализации:

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

}

Повторного заказ вид поддержки аксессуара отображается, если вы реализовали moveRowAtIndexPath как, для вашего случая:

- (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]; 

} 

где getDataSourceArrayFromIndexPath:

-(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; 
} 
0

Используйте этот код

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 
{ 
    NSMutableArray *arrayTemp=[arrayMain objectAtIndex:sourceIndexPath.row]; 
    [arrayVisibleCountryList removeObjectAtIndex:sourceIndexPath.row]; 
    [arrayVisibleCountryList insertObject:arrayTemp atIndex:destinationIndexPath.row]; 
} 
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath 
{ 
    //Uncomment these lines to enable moving a row just within it's current section 
    if ([sourceIndexPath section] != [proposedDestinationIndexPath section]) 
    { 
     proposedDestinationIndexPath = sourceIndexPath; 
    } 

    return proposedDestinationIndexPath; 
} 
+1

Что делает этот код? Как это работает? И откуда вы его скопировали? –

0

Вы можете использовать ниже метод Tableview. Он перемещает вашу ячейку от sourceIndexPath до destinationIndexPath.

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