我遇到了一个问题,即在重复使用单元格时会添加重复的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;
}
答案 0 :(得分:2)
你的问题是表视图中的可重用单元格,试试这段代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
textField.tag=101;
[cell addSubview:textField];
UIButton *sendFeedback = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
sendFeedback.tag=102;
[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];
}
UITextField* textField=(UITextField*)[cell viewWithTag:101];
UIButton *sendFeedback = (UIButton*)[cell viewWithTag:102];
sendFeedback.hidden=YES;
textField.hidden=NO;
[textField setKeyboardType:UIKeyboardTypeDefault];
if (indexPath.section == 0)
{
[textField setPlaceholder:@"Product"];
if ([productData length] > 0)
{
[textField setText:productData];
[textField setEnabled:false];
}
} else if (indexPath.section == 1){
[textField setPlaceholder:@"What's the issue?"];
[textField setAutocorrectionType:UITextAutocorrectionTypeNo];
} else if (indexPath.section == 2){
[textField setPlaceholder:@"Email address"];
[textField setAutocorrectionType:UITextAutocorrectionTypeNo];
[textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[textField setKeyboardType:UIKeyboardTypeEmailAddress];
} else if (indexPath.section == 3){
[textField setPlaceholder:@"Any notes to add?"];
[textField setAutocorrectionType:UITextAutocorrectionTypeNo];
} else if (indexPath.section == 4)
{
textField.hidden=YES;
sendFeedback.hidden=NO;
}
return cell;
}
//您不能在表视图中直接访问textField.text,使用全局变量或字典分配textField.text
答案 1 :(得分:0)
这种情况正在发生,因为您继续将子视图添加到单元格中。例如,您将productField
添加到索引0处的单元格。当重复使用该单元格时,您将向其添加另一个子视图,但productField
仍然存在。一种可能的解决方案是在添加新视图之前删除子视图。您的if
语句将是这样的:
if (indexPath.section == 0){
// Remove the old subview
UIView *oldSubview = [cell viewWithTag:5];
[oldSubview removeFromSuperview];
productField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[productField setPlaceholder:@"Product"];
if ([productData length] > 0){
[productField setText:productData];
[productField setEnabled:false];
}
// Set a tag for the new subview, so you can remove it later
productField.tag = 5;
[cell addSubview:productField];
}
在每个tag
语句中使用相同的if
。
修改强>
或者你可以像@pawan建议一样使用静态单元格。
答案 2 :(得分:0)
问题在于,由于您正在回收单元格(并且您应该),因此您将重复使用非空单元格并在其上添加新控件。
处理它的常用方法是构建自定义UITableViewCell子类,并将各种文本字段和其他UI控件作为属性。 然后,您可以在每个部分中显示/隐藏这些UI控件
它看起来像那样:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (indexPath.section == 0){
productField.hidden=NO;
issueField.hidden=YES;
} else if (indexPath.section == 1){
productField.hidden=YES;
issueField.hidden=NO;
}
return cell;
}
答案 3 :(得分:0)
为什么在实例化单元格后,从单元格contentView
中删除所有子视图for( UIView *view in cell.contentView.subviews )
[view removeFromSuperview];
答案 4 :(得分:0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
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;
}