2013-07-10 99 views
1

我有一個具有UITableView的UIViewController,最多可以有7個單元格。我已經設置了選項cell.showsReorderControl = YES;,用戶可以重新排列單元格。在創建單元格時,我還包含了cell.tag。iOS:在用戶重新排列單元格後查找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 
+0

我不確定問題是什麼,你有什麼試圖得到它? – micantox

+0

我還沒有嘗試過任何東西。我不確定如何在用戶重新組合單元格後獲取每個單元格的標記值。 – user1107173

回答

1

實際添加一個NSMutableArray.h文件。

現在viewDidLoad方法intialize

arrayTag = [NSMutableArray array]; 

雖然adding cell's tagarray

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

編輯:你的單元格的順序與它的標籤.........

+0

謝謝王子。這看起來不錯。我會在幾個小時內嘗試這個,並將其標記爲正確。謝謝! – user1107173

+0

我更新了上面的代碼。記錄AFTER移動/混排單元格後,我沒有得到唯一的值。如果你看看上面的日誌,你會注意到當我不移動任何單元格時,它會按照唯一值顯示單元格。但是,在混洗/移動單元之後,有多個具有相同值的單元格?謝謝。 – user1107173

相關問題