Ios 根据表中显示的内容重新排序可变数组
我有一个dictionary(NSMutableDictionary)的数组(NSMutableArray),它存储图像和一些字符串Ios 根据表中显示的内容重新排序可变数组,ios,uitableview,ios6,nsmutablearray,nsmutabledictionary,Ios,Uitableview,Ios6,Nsmutablearray,Nsmutabledictionary,我有一个dictionary(NSMutableDictionary)的数组(NSMutableArray),它存储图像和一些字符串 @interface SelectViewController () { NSMutableArray *pictureArray; NSMutableDictionary *dict1; UIImage *key1; // UIImage NSString *key2; // file name NSString *key
@interface SelectViewController () {
NSMutableArray *pictureArray;
NSMutableDictionary *dict1;
UIImage *key1; // UIImage
NSString *key2; // file name
NSString *key3; // file path
NSString *key4; // description
NSString *key5; // order
}
我想让用户重新排序行
- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
UIImage *image = [[pictureArray objectAtIndex:indexPath.row] objectForKey:key1];
cell.imageView.image = image;
NSString *filename = [[pictureArray objectAtIndex:indexPath.row] objectForKey:key2];
cell.textLabel.text = filename;
cell.showsReorderControl = YES;
return cell;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSLog(@"From %i to %i",fromIndexPath.row,toIndexPath.row);
}
因此,每个表行上都会出现一个灰色符号,用户可以对行重新排序。我的问题是如何根据表中显示的内容对数组(pictureArray)重新排序?我将此数组传递给另一个类。阵列的出现顺序尚未改变
谢谢你的建议
- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)targetIndexPath
{
NSUInteger sourceIndex = [sourceIndexPath row];
NSUInteger targetIndex = [targetIndexPath row];
if (sourceIndex != targetIndex)
{
NSString *item = [pictureArray objectAtIndex:sourceIndex];
[pictureArray removeObject:item];
[pictureArray insertObject:item atIndex:targetIndex];
}
}
把这个放好哦,天哪。。。就这么简单!?谢谢。我会在3分钟内核对并接受答案。很高兴您理解了我的需要。@Manohar+1您能用这个代码做同样的事情吗[pictureArray exchangeObjectAtIndex:sourceIndex withObjectAtIndex:targetIndex];如果可以的话,它会更优雅,看起来你的代码会稍微打破外观的顺序。很可能,其中一个应该被删除的行没有被删除。