获取重新排序的 UITableViewCells 的顺序 [英] Getting the order of reordered UITableViewCells

查看:61
本文介绍了获取重新排序的 UITableViewCells 的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将单元格的重新排序索引放入数组中.

I'm trying to get reordered index of cells into an array.

viewDidLoad

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

//创建单元格时将indexPath.row保存为cell.tag.

//Saving indexPath.row as cell.tag while creating the cell.

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

//我不确定这是否是正确的方法,但在下面我试图将更改保存到数组中.

//I'm not sure if this is the right way, but below I'm trying to save the changes to the array.

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

验证我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 没有按预期移动单元格结果.

NSLog WITHOUT moving cells results as expected.

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 移动/改组单元格后似乎是错误的.我没有得到唯一值.

NSLog AFTER moving/shuffling cells appears to be wrong. I'm not getting unique values.

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: Why am I not getting new unique values after reordering? How do I get the new order of cells saved in the array?

推荐答案

我终于找到了一个适用于 Apple 文档的解决方案.

I finally found a solution that works in Apple's Docs.

声明一个 NSMutableArray 属性 .h 或 .m: @property (strong, nonatomic) NSMutableArray *arrayTag;

Declare a NSMutableArray property .h or .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

Link to Apple's site: Listing 8-2: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/ManageReorderRow/ManageReorderRow.html

这篇关于获取重新排序的 UITableViewCells 的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆