2011-11-01 106 views
0

之前我問這個問題我搜索了很多,看到一些答案,但沒有人可以幫助我,所以我決定再問一次,...感謝您的任何幫助自定義TableViewCell值滾動時消失

我知道有解決這個問題有兩種方式,一種是存儲在陣列tableviewcell值,另一個是避免重複使用它,但我可以NT實現它們,

我有一個具有內部一個文本框custome tableviewcell和一個按鈕,當一個按鈕點擊一個添加另一個tableviewcell在tableview的底部;

當我實施第一個解決方案,我得到的tableviewcell從textfield.supperview這樣的:

-(void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    NSLog(@"TextFileld Value : %@" , textField.text); 

    DivideTableViewCell *cell = (DivideTableViewCell *)textField.superview; 

    NSIndexPath *indexPath = [uiTableView indexPathForCell:cell]; 

    NSLog(@"index Path %i" , indexPath.row); 
} 

但每次indexPath.row是0,我可以NT更新我的數據源陣列....

,也很遺憾我無法實現第二個解決方案太...

所以感謝您的幫助:)

回答

0

如果你需要存儲TableViewCells嘗試以下操作。

擁有一個NSMutableDictionary實例變量並在TableViewController的init方法中分配/ init它(不要忘記在dealloc中釋放它)。

- (id)init { 
    self = [super init]; 
    if (self) { 
    ivarDictionary = [[NSMutableDictionary alloc] init]; 
    } 
    return self; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    DivideTableViewCell *cell = [ivarDictionary objectForKey:[NSNumber numberWithInt:indexPath.row]]; 

    if (!cell) { 
    cell = [[DivideTableViewCell alloc] init]; 
    [iverDictionary setObject:cell forKey:[NSNumber numberWithInt:indexPath.row]]; 
    // Cell setup 1 
    } 

    // Cell setup 2 

    return cell; 
} 

從那裏,你可以設置你需要在設置1範圍內的小區,或者如果你想每次都做一些設置細胞就會被召回,然後使用安裝程序2.

請注意,這在減少內存使用方面不是一個好主意,我不會用這種方法來處理20-30個以上的單元。

您仍然需要使用TextField委託方法來提取輸入的文本,您可能還需要在每個單元格創建時分析indexPath變量,以便您可以知道正在編輯哪個文本字段。

+0

我想,這可能泄漏的細胞內存,您可能需要一個自動添加到DivideTableViewCell分配/初始化線 – Littlejon

+0

您好感謝您的關注,但不幸的是它NT工作對我來說,我不不知道我在哪裏做錯了什麼,讓我們改變目標,我可以從textFieldDidEndEditing獲取文本,我如何獲得該tableviewcell的textfielld值更改的索引路徑? 我想如果我能得到正確的索引路徑,我可以使我的數組數據源更新,一切都會好的。 謝謝 – ali

+0

@ali你需要添加一個ivar到你的DivideTableViewCell中,記錄它顯示的行。確保每次使用tableView顯示單元格時更新此值:cellForRowAtIndexPath: – Littlejon

0

 
I Think you should store all the values of the text field in an array and replace with the changed value every time like i did,see my code ,

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; {
    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; for (id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[UITableViewCell class]]) { cell = (CustomCell ) currentObject; break; } } } cell.capitalLabel.text =[capitals objectAtIndex:indexPath.row]; cell.stateLabel.text = [states objectAtIndex:indexPath.row]; cell.t1.delegate=self; cell.t1.tag=indexPath.row; cell.t1.text=[arrTemp objectAtIndex:indexPath.row]; cell.s1.backgroundColor=[UIColor grayColor]; cell.s1.contentSize=CGSizeMake(1000, 40); return cell; } } /-(BOOL)textFieldShouldReturn:(UITextField *)textField { [arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text]; } */ -(void)textFieldDidEndEditing:(UITextField *)textField{ [arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text]; }

+0

我已經使用了自定義單元格! –