2011-01-08 65 views
2

我有一個tableView,其中包含一個UITextField的單元格作爲每個單元格的子視圖。我的問題是,當我向下滾動時,第一個單元格中的文本被複制到最後一個單元格中。如果我弄清楚爲什麼我不能爲了生活。我嘗試從不同的筆尖加載單元格,將textFields作爲ivars。 UITextFields似乎不是問題,我認爲它與tableView重用單元格有關。第一個和最後一個UITableViewCell在滾動時保持不變

textFields都有一個數據源,用於跟蹤textField中的文本,並在每次顯示單元格時重置文本。

任何想法?我真的很感謝這個問題的更多答案。

更新2: 這是我有一個自定義單元格的代碼,稱爲JournalCell。真的很欣賞反饋。

我有8個部分各1行。前7個在其中有一個textField,最後一個是一個像按鈕一樣的單元。

我正在測試按鈕單元格,如果它匹配的部分(7),然後它返回該單元格,如果不是,它繼續其餘的。這可能嗎?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

NSLog(@"Section %i, Row %i", indexPath.section, indexPath.row); 


if (indexPath.section == 7) { 

    static NSString *ButtonCellIdentifier = @"ButtonCellIdentifier"; 

    UITableViewCell *buttonCell = [self.tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier]; 

    if (buttonCell == nil) { 
     buttonCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ButtonCellIdentifier] autorelease]; 
     buttonCell.selectionStyle = UITableViewCellSelectionStyleBlue; 
     buttonCell.accessoryType = UITableViewCellAccessoryNone; 
     buttonCell.textLabel.text = sClearAll; 
     buttonCell.textLabel.textAlignment = UITextAlignmentCenter; 
     buttonCell.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8]; 
     buttonCell.textLabel.backgroundColor = [UIColor clearColor]; 
    } 

    return buttonCell; 
} 

static NSString *TextCellIdentifier = @"JournalCellIdentifier"; 

JournalCell *cell = (JournalCell *)[self.tableView dequeueReusableCellWithIdentifier:TextCellIdentifier]; 
if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"JournalCell" owner:self options:nil]; 
    cell = customCell; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    cell.textField.autocapitalizationType = UITextAutocapitalizationTypeWords; 
    cell.textField.returnKeyType = UIReturnKeyNext; 
    cell.textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
} 

switch (indexPath.section) { 
    case 0: 
     switch (indexPath.row) { 
      case 0: 
       cell.textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
       self.authorTextField = cell.textField; 
       self.authorTextField.text = [self.textFieldDictionary objectForKey:@"author"]; 
       NSLog(@"Reading Author:%@", [self.textFieldDictionary objectForKey:@"author"]); 
       break; 
     } 
     break; 

    case 1: 
     switch (indexPath.row) { 
      case 0: 
       self.yearTextField = cell.textField; 
       self.yearTextField.text = [self.textFieldDictionary objectForKey:@"year"]; 
       NSLog(@"Reading Year:%@", [self.textFieldDictionary objectForKey:@"year"]); 
       break;     
     } 
     break; 

    case 2: 
     switch (indexPath.row) { 
      case 0: 
       self.volumeTextField = cell.textField; 
       self.volumeTextField.text = [self.textFieldDictionary objectForKey:@"volume"]; 
       NSLog(@"Reading Volume:%@", [self.textFieldDictionary objectForKey:@"volume"]); 
       break;      
     } 
     break; 

    case 3: 
     switch (indexPath.row) { 
      case 0: 
       self.articleTextField = cell.textField; 
       self.articleTextField.text = [self.textFieldDictionary objectForKey:@"article"]; 
       NSLog(@"Reading Article:%@", [self.textFieldDictionary objectForKey:@"article"]); 
       break;   
     } 
     break; 

    default: 
     break; 
} 

return cell; 

}

+1

提供了一些示例代碼,以便我們可以找到問題;) – 2011-01-08 20:06:24

+0

您在正確重用單元時遇到了一些問題。發佈您的cellForRowAtIndexPath:方法,以便我們可以幫助修復它 – Vladimir 2011-01-08 20:15:35

+1

我更新了示例代碼以反映我用於自定義單元格的內容。 – 2011-01-09 03:29:10

回答

4

正如你所猜測的,問題是最可能來自UITableView中單元的傳統重用。創建一個NSMutableDictionary作爲你的類的屬性,並且每當UITextField完成編輯時,將它的值設置爲字典中的一個鍵。

然後,在您cellForRowAtIndexPath方法中,加載在字典中的相應項的值。

這將是最理想的名字在你的字典與格式[indexPath section], [indexPath row]每個關鍵,因爲這將在設置幫助和獲取價值。

希望這會有所幫助。

0

嘗試allocing細胞後,做任何繪圖/標籤/等之前插入這一權利。

cell.clearsContextBeforeDrawing = YES; 

cell.textLabel.text = nil; 
cell.detailTextLabel.text = nil; 

for (UIView *view in cell.contentView.subviews) { 
    [view removeFromSuperview]; 
} 
2

加拿大開發的答案可能會解決您的問題,但它只是一個真正的缺口而不是長期的修復。

您應該避免在cellForRowAtIndexPath:方法中添加子視圖。

您應該改爲創建一個自定義的UITableViewCell(通常是通過繼承UITableViewCell)並使用它 - 否則您將在細胞出列時添加/刪除子視圖。

如果您不確定細胞出列的實際含義,您應該閱讀Apple的TableView文檔。基本上,使用一個UITableViewCell子類,你會比在你的cellForRow方法中添加所有視圖更好。

2

您是否真的需要將每個單元格和textField保存到您的viewController,然後通過插入其他單元格的部分來生成單元格?

也許你可以保存整個小區的物業(如self.authorCell),然後簡單地顯示該單元格:

if (indexPath.section == 0) { 
    return self.authorCell; // or cell = self.authorCell 
} 

,你仍然可以通過訪問單元格屬性訪問textField

self.authorCell.myTextField 

//編輯:參見蘋果documentation「靜態行內容的技術」