1

我有一个 UIViewController 和一个 UITableView 最多可以有 7 个单元格。我已经设置了选项cell.showsReorderControl = YES;,用户可以重新排序单元格。在创建单元格时,我还包含了 cell.tag。

用户重新排序单元格后,我想知道表格中单元格的排列。为了做到这一点,我想我会设置标签(不确定这是否是正确的方法)。

所以我的问题是在prepareForSegue中,在用户重新排列单元格之后,我想知道每个单元格的标签值是什么?

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

不移动单元格记录:

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

移动/改组单元格后记录:

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
4

1 回答 1

1

实际上NSMutableArray.h文件中添加一个。

现在在viewDidLoad方法intialize

arrayTag = [NSMutableArray array];

adding cell's tag添加时array

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

现在在这些delegate方法replace标签中

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

编辑:您有带有标签的单元格的顺序............

于 2013-07-10T13:05:40.237 回答