2016-10-20 3 views
0

私のiOSアプリケーションでは、配列(dataArray)を使用するセクションから別の配列(followArray)を使用する別のセクションに、各セル内のボタンでセルを移動しようとしています。ボタンは設定されていますが、ボタン操作でセルを移動するコードは見つかりません。ボタンで2つのセクション間でセルを移動するにはどうすればよいですか?

私はNSMutableArrayのからオブジェクトを追加および削除するにはいくつかのコードを追加してみましたが、私はエラー


***キャッチされない例外が原因アプリ「NSInternalInconsistencyException」を終了しました、理由:「を挿入しようとする試みセクション0に行0が、セクション0でのみ0行が更新」


コードになった後ある - (ボイド) followButtonClick:(ID)差出人ここ

は、私は、サーバーからの私の配列を取得し、手動で入力していないので、タグによって各セルに入り、私は私のボタンに追加してみましたコードである

 [myTableView beginUpdates]; 

    // Removing Cell from dataArray Section 
    [dataArray removeObject: indexPath]; 
    NSInteger rowToRemove = indexPath.row; 
    [myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES]; 

    // Inserting Cell to followedArray Section 
    [followedArray insertObject:indexPath atIndex:indexPath.row]; 
    NSInteger rowToAdd = indexPath.row; 
    [myTableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES]; 

    [myTableView endUpdates]; 

これは多くのレベルで間違っていると確信していますが、ここでは空白を描いています。うまくいけば、誰かが正しい方向に私を向けることができます。

コード:

-(void)followButtonClick:(UIButton *)sender { 

// Adding row to tag 
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView]; 
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition]; 

// Creating an action per tag 
if (indexPath != nil) 
{ 
    NSLog(@"Current Row = %@", indexPath); 

    // Showing Status Labels 
    CustomCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath]; 
    cell.firstStatusLabel.hidden = NO; 
    cell.secondStatusLabel.hidden = NO; 

    // Change Follow to Following 
    [sender setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal]; 
    cell.followButton.hidden = YES; 
    cell.followedButton.hidden = NO; 

    // ----- ERROR HERE ----- 
    [self.myTableView beginUpdates]; 

    // ----- Inserting Cell to Section 0 ----- *NOT WORKING* 
    [followedArray insertObject:[gamesArray objectAtIndex:indexPath.row] atIndex:indexPath.row]; 
    NSInteger rowToAdd = indexPath.row; 
    [self.myTableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES]; 

    // ----- Removing Cell from Section 1 ----- *WORKING* 
    [gamesArray removeObjectAtIndex:indexPath.row]; 
    NSInteger rowToRemove = indexPath.row; 
    [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES]; 

    [self.myTableView endUpdates]; 
    } 
} 

filteredArrayは私のためにある各セル内の

cellForRowAtIndexPath

// Configuring the cell 
Data * dataObject; 
if (!isFiltered) { 

    if (indexPath.section == 0) { 
     dataObject = [followedArray objectAtIndex:indexPath.row]; 
    } 
    else { 
     dataObject = [dataArray objectAtIndex:indexPath.row]; 
    } 
} 
else { 
    dataObject = [filteredArray objectAtIndex:indexPath.row]; 
} 

cell.followButton.tag = indexPath.row; 
[cell.followButton addTarget:self action:@selector(followButtonClick:) forControlEvents:UIControlEventTouchUpInside]; 
cell.followButton.hidden = NO; 

フォローボタンをクリック検索バーでは、項目はdataArrayからフィルタリングされます。すべての私のデータは私のサーバー

からである場合

dataArrayがある私は、ボタンがdataArrayまたはfilteredArrayからクリックされたときにセルを配置する場所followedArrayです。

コードがさらに必要な場合は、うまくいけば、私はこれを解決することができます、ありがとう!

答えて

0

これは私がエラーを修正助けたものです、クレジット@AliOmari & @Rachelへ

[self.myTableView beginUpdates]; 

    [followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:0]; 
    [myTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; 

    [dataArray removeObjectAtIndex:indexPath.row]; 
    NSInteger rowToRemove = indexPath.row; 
    [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES]; 

    [self.myTableView endUpdates]; 
1

セルを1つのセクションから削除し、そのセルを別のセクションに挿入することができます。

//update data Array1 
[xxx removeObject:xxx]; 
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:rowToRemove inSection:0]] withRowAnimation:YES]; 

//update data Array2 
[xxx insertObject:xxx]; 
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:rowToAdd inSection:0]] withRowAnimation: YES]; 
+0

私はは、insertObjectのエラーを取得しています:、「NSArrayのために目に見える@interfaceは、セレクタを宣言していないは、insertObject : " – BroSimple

+1

@BroSimple NSArrayは変更できません。そのため、変更することはできません。 NSMutableArrayを使用できます。以下の例を参照してください。 [anArray removeObjectAtIndex:index]; [anArray removeObject:item]; [anArray removeLastObject]; – Rachel

+0

これでNSMutableArrayを作ったので、[followArray insertObject:indexPath atIndex:0];を使用しました。 indexPathForRow:rowToRemoveまたはrowToAddの場合、これは正解です。これはどこで使用しますか?私はかなり新しい申し訳ありません – BroSimple

関連する問題