並べ替えられたセルのインデックスを配列に取得しようとしています。
の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
NSLog
AFTER セルの移動/シャッフルが間違っているようです。一意の値が得られません。
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: 再注文後に新しい一意の値を取得できないのはなぜですか? 配列に保存されたセルの新しい順序を取得するにはどうすればよいですか?