2012-07-08 84 views
0

我有一個包含UITextField的自定義UITableViewCell原型。在最後一節填寫UITextField時,我會自動添加新的單元格。哪裏有更多的部分不適合在一個屏幕上,我添加另一個部分,它會充滿第一部分的值,並且第一部分輸入被清空!當視圖滾動屏幕時,UITextField內容會丟失

截圖:

first step

added fourth row - it contains a!

first row is empty!

相關代碼:

@implementation TMNewTripPeopleViewController 

@synthesize sectionsCount = _sectionsCount; 
@synthesize firstCellShowed = _firstCellShowed; 

- (int) sectionsCount 
{ 
    if (_sectionsCount == 0) { 
     _sectionsCount = 1; 
    } 
    return _sectionsCount; 
} 

- (IBAction)inputChanged:(id)sender { 
    UITextField* input = (UITextField*) sender; 
    NSIndexPath* indexPathName = [NSIndexPath indexPathForRow:0 inSection:input.tag - 1]; 
    UITableViewCell* cellName = [self.tableView cellForRowAtIndexPath:indexPathName]; 

    if (input == [cellName viewWithTag:input.tag]) { 
     // last name input - add next section? 
     if (input.tag == self.sectionsCount) { 
      if (input.text.length > 0) { 
       self.sectionsCount++; 
       [self.tableView insertSections:[NSIndexSet indexSetWithIndex:self.sectionsCount - 1] withRowAnimation:UITableViewRowAnimationTop]; 
      } 
     } 
    } 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return self.sectionsCount; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return 2; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier = @"PersonName"; 
    if (indexPath.row % 2 == 1) { 
     CellIdentifier = @"PersonEmail"; 
    } 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UITextField* input = (UITextField*) [cell viewWithTag:indexPath.section + 1]; 
    if (indexPath.row == 0 && indexPath.section == 0 && !self.firstCellShowed) { 
     [input becomeFirstResponder]; 
     self.firstCellShowed = YES; 
    } 

    [cell viewWithTag:1].tag = indexPath.section + 1; 

    return cell; 
} 

@end 

回答

1

我不在你看是否履行tableView:willDisplayCell:forRowAtIndexPath:離子。您通常使用此方法設置單元格的顯示值。

當你使用dequeueReusableCellWithIdentifier(你幾乎總是應該),那麼你的單元格將在表格視圖滾動時被重用。如果你不更新willDisplayCell的值,那麼他們會顯示他們以前有任何值重用之前(如果有的話)。

+0

所以我應該保存這些值在一個模型關聯的單元格行+部分每次他們改變? – 2012-07-08 13:00:56

+0

是的,確實如此。你可能想結賬。 http://stackoverflow.com/questions/4561424/uitableview-tutorial – logancautrell 2012-07-08 23:51:39

相關問題