iOS:用户重新排序单元格后查找cell.tag值

时间:2013-07-10 12:50:05

标签: ios uitableview

我有一个带有UITableView的UIViewController,最多可以有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

1 个答案:

答案 0 :(得分: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];
}

编辑:您的单元格的顺序为.........