【发布时间】:2014-02-11 14:59:20
【问题描述】:
我遇到了一个问题,即重复使用单元格时添加了重复的 UITextField,这不是我想要发生的。我记得以前遇到过类似的问题,但是对于我的生活,我不记得我做了什么来解决它。
毫无疑问,这很明显,但我似乎找不到任何有用的东西。正如某些人所建议的那样,我已经尝试将 if/else 语句包含在“if (cell == null)”中,但这只会导致形成一个空白表。
我们将不胜感激一些建议。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (indexPath.section == 0){
productField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[productField setPlaceholder:@"Product"];
if ([productData length] > 0){
[productField setText:productData];
[productField setEnabled:false];
}
[cell addSubview:productField];
} else if (indexPath.section == 1){
issueField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[issueField setPlaceholder:@"What's the issue?"];
[issueField setAutocorrectionType:UITextAutocorrectionTypeNo];
[cell addSubview:issueField];
} else if (indexPath.section == 2){
emailField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[emailField setPlaceholder:@"Email address"];
[emailField setAutocorrectionType:UITextAutocorrectionTypeNo];
[emailField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[emailField setKeyboardType:UIKeyboardTypeEmailAddress];
[cell addSubview:emailField];
} else if (indexPath.section == 3){
notesField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[notesField setPlaceholder:@"Any notes to add?"];
[notesField setAutocorrectionType:UITextAutocorrectionTypeNo];
[cell addSubview:notesField];
} else if (indexPath.section == 4){
sendFeedback = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[sendFeedback setTitle:@"Send Feedback" forState:UIControlStateNormal];
[sendFeedback setBackgroundColor:[UIColor colorWithRed:(111/255.0f) green:(31/255.0f) blue:(68/255.0f) alpha:1.0f]];
[sendFeedback.titleLabel setFont:[UIFont fontWithName:@"MaryAnn" size:20.0]];
[sendFeedback addTarget:self action:@selector(sendFeedback:) forControlEvents:UIControlEventTouchUpInside];
[cell setBackgroundColor:[UIColor clearColor]];
[cell addSubview:sendFeedback];
}
return cell;
}
【问题讨论】:
-
你应该使用静态单元格代替 dequeueReusableCell 它们。
-
你不能创建自定义单元格吗?
-
感谢您的帮助。我选择了静态单元格(完全忘记了它们),因为它只会在测试版本中使用,而不是在应用程序的生产版本中使用。
标签: ios objective-c uitableview uitextfield