2012-01-03 64 views
1

在我的應用程序需要能夠跟蹤識別的UITextField來自哪個小區的時候我犯textFieldDidEndEditing:編輯修改。爲此,我只需在tableview:cellForRowAtIndexPath:單元格的文本字段上設置標籤屬性即可。一行之後更新的UITableViewCell標籤財產轉移

當單元格移動時,這會導致一些麻煩。例如,如果我將底部單元格移到頂部,請刪除現在位於底部的單元格,然後嘗試編輯剛剛放置在頂部的單元格,我的應用程序將因爲標籤屬性仍然爲8而崩潰(或另一個數字),但是我的數據源中沒有這麼多元素。

我試圖通過更新標籤中tableView:moveRowAtIndexPath:toIndexPath:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
    NSString *siteToMove = [[sites objectAtIndex:[fromIndexPath row]] retain]; 

    [sites removeObjectAtIndex:[fromIndexPath row]]; 
    [sites insertObject:siteToMove atIndex:[toIndexPath row]]; 

    if ([toIndexPath section] == 1) { 
     [[(BookmarkTextEntryTableViewCell *)[[self tableView] cellForRowAtIndexPath:toIndexPath] textField] setTag:[toIndexPath row]]; 
    } 
} 

來解決這個問題。然而,這並不工作,因爲(BookmarkTextEntryTableViewCell *)[[self tableView] cellForRowAtIndexPath:toIndexPath]返回是移動行有之前細胞。

回答

1

在你- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath方法,你可以通過你可能已經添加爲子視圖表中的所有UITextField小號迭代,然後檢查和設置AS-

for (UIView* subview in [[self tableView] subviews]) { 
if ([subview isKindOfClass:[UITextField class]]) { 
    if(subview.tag==oldTag){ 
    subview.tag = newTag; 
    break; 
    } 
} 
} 

編輯新標籤:關於第二個想法,您可以重新加載表格數據,以便重置所有剩餘單元格的標籤。