2016-07-22 2 views
0

こんにちは皆私はiOSの初心者です。移動UITableViewCell

UITableViewControllerに3セクションのUITableViewを実装しました。そして、私は各セクションにセクションの行を表示しました。私はこれらの3つのセクション間の行を移動したい

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

今私の出力は次のようになります。どうすればそれをすることができます..?私はそれをやろうとしている間、行はセクション間を移動しますが、保存すると問題が生じます。

どうすればいいですか?

おかげ

答えて

0

あなたはUITableViewの再注文能力を利用する必要があります。セルはセクション間で並べ替えることができます。 並べ替えモードを有効にするには:

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

をこれは、並べ替えと一緒に削除モードを有効に含まれる編集モードでテーブルビューを可能にします。

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

} 

:あなたは、あなたのケースのために、同様moveRowAtIndexPathを実装している場合

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

}

再オーダーサポートアクセサリビューが示されている:あなたは、削除モードを実装したくない場合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

は、このコードあなたがのtableViewメソッドの下に使用することができます

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

を使用してください。セルをsourceIndexPathからdestinationIndexPathに移動します。

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath; 
関連する問題