gpt4 book ai didi

iOS UITableView 单元格重复

转载 作者:行者123 更新时间:2023-11-28 23:47:31 25 4
gpt4 key购买 nike

我有一个 tableviewcontroller,它在单元格中创建了动态控件。如果是下拉类型,我会将用户带到不同的 tableviewcontroller 来选择值。选择后,我会弹出并重新加载数据,但是当我这样做时,它会一层一层地覆盖单元格。我知道这是因为我重复使用了细胞,但我似乎不知道如何防止它。

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];

[self.tableView reloadData];

}


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


EWHInboundCustomAttribute *ca = [visibleCustomAttributes objectAtIndex:indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.tag=indexPath.row;


if (ca.CustomControlType == 1) {
cell.detailTextLabel.hidden=true;
cell.textLabel.hidden=true;

UITextField *caTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)];

caTextField.adjustsFontSizeToFitWidth = YES;
caTextField.textColor = [UIColor blackColor];

caTextField.placeholder = ca.LabelCaption;
if (ca.ReadOnly) {
[caTextField setEnabled: NO];
} else {
[caTextField setEnabled: YES];
}
caTextField.text=nil;
caTextField.text=ca.Value;
caTextField.tag=indexPath.row;

caTextField.delegate=self;

[cell.contentView addSubview:caTextField];



} else if (ca.CustomControlType == 4) {

cell.detailTextLabel.text=ca.Value;
cell.textLabel.text=ca.LabelCaption;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

} else {

cell.detailTextLabel.hidden=true;
cell.textLabel.hidden=true;
UITextField *caTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)];
caTextField.adjustsFontSizeToFitWidth = YES;
caTextField.textColor = [UIColor grayColor];

caTextField.placeholder = ca.LabelCaption;
[caTextField setEnabled: NO];
caTextField.text = ca.Value;

caTextField.tag=indexPath.row;
caTextField.delegate=self;
[cell.contentView addSubview:caTextField];
}



return cell;
}

enter image description here

最佳答案

与其每次都创建 UITextfield,我建议至少使用 [UIView viewWithTag:tag] 来捕获相同的 UITextField 对象。

关于iOS UITableView 单元格重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52414066/

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