我有一個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;
}
提供了一些示例代碼,以便我們可以找到問題;) – 2011-01-08 20:06:24
您在正確重用單元時遇到了一些問題。發佈您的cellForRowAtIndexPath:方法,以便我們可以幫助修復它 – Vladimir 2011-01-08 20:15:35
我更新了示例代碼以反映我用於自定義單元格的內容。 – 2011-01-09 03:29:10