1

並べ替えられたセルのインデックスを配列に取得しようとしています。

viewDidLoad

cell.showsReorderControl = YES;
arrayTag = [NSMutableArray array];

//セルの作成中に indexPath.row を cell.tag として保存します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
 {
   .....

   cell.tag = indexPath.row;
   NSString *strCellTag = [NSString stringWithFormat:@"%d",cell.tag];
   if(![arrayTag containsObject:strCellTag])
   {
      [arrayTag addObject:strCellTag];
   }
   return cell;
 }

//これが正しい方法かどうかはわかりませんが、以下では変更を配列に保存しようとしています。

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    [arrayTag replaceObjectAtIndex:sourceIndexPath.row withObject:[NSString stringWithFormat:@"%i",proposedDestinationIndexPath.row]];
    [arrayTag replaceObjectAtIndex:proposedDestinationIndexPath.row withObject:[NSString stringWithFormat:@"%i",sourceIndexPath.row]];

    return proposedDestinationIndexPath;
}

I を確認するにはNSLog:

- (IBAction)doneButton:(id)sender
{
    NSLog (@"Number of Objects in Array %i", arrayTag.count);

    for (NSString *obj in arrayTag){
        NSLog(@"From ArrayTag obj: %@", obj);
    }   
}

NSLogセルを移動しないと、期待どおりの結果が得られます。

2013-07-10 17:50:47.291 MyApp[1634:c07] Number of Objects in Array 7
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 0
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 1
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 2
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 3
2013-07-10 17:50:47.293 MyApp[1634:c07] From ArrayTag obj: 4
2013-07-10 17:50:47.293 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:50:47.293 MyApp[1634:c07] From ArrayTag obj: 6

NSLogAFTER セルの移動/シャッフルが間違っているようです。一意の値が得られません。

2013-07-10 17:51:55.329 MyApp[1634:c07] Number of Objects in Array 7
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 4
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:51:55.331 MyApp[1634:c07] From ArrayTag obj: 6
2013-07-10 17:51:55.331 MyApp[1634:c07] From ArrayTag obj: 6
2013-07-10 17:51:55.331 MyApp[1634:c07] From ArrayTag obj: 4

Q: 再注文後に新しい一意の値を取得できないのはなぜですか? 配列に保存されたセルの新しい順序を取得するにはどうすればよいですか?

4

3 に答える 3

3

AppleのDocsで機能するソリューションをついに見つけました。

NSMutableArray プロパティ .h または .m を宣言します。@property (strong, nonatomic) NSMutableArray *arrayTag;

それを合成する:@synthesize arrayTag;

で初期化しますviewDidLoad arrayTag = [[NSMutableArray alloc] init];

コードは次のとおりです。

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSString *stringToMove = [arrayTag objectAtIndex:sourceIndexPath.row];
    [arrayTag removeObjectAtIndex:sourceIndexPath.row];
    [arrayTag insertObject:stringToMove atIndex:destinationIndexPath.row];
}

Apple のサイトへのリンク: リスト 8-2: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/ManageReorderRow/ManageReorderRow.html

于 2013-07-13T20:53:38.907 に答える
0

2 つのオブジェクトを交換する場合は、オブジェクトを一時的な場所に保持する必要があります。

疑似コード

arr = [a,b,c]

//to swap(0,2)...

tmp = arr[2]

[arr replaceObjectAtIndex: 2 withObject: arr[0]]

[arr replaceObjectAtIndex: 0 withObject:tmp];
于 2013-07-11T19:35:16.930 に答える