gpt4 book ai didi

ios - tableView 滚动时添加重复的 UITextField

转载 作者:行者123 更新时间:2023-11-29 10:48:02 25 4
gpt4 key购买 nike

我遇到了一个问题,即在重新使用单元格时添加了重复的 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;
}

最佳答案

你的问题是表格 View 中的可重用单元格,试试这个代码

- (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;
}

//你不能直接在 TableView 中访问 textField.text,你使用全局变量或字典分配 textField.text

关于ios - tableView 滚动时添加重复的 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21705350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com