gpt4 book ai didi

ios - UiTextField 在 UtableView 中使用时会造成数据困惑。

转载 作者:行者123 更新时间:2023-12-01 17:37:21 25 4
gpt4 key购买 nike

我用一个 textField 创建了一个简单的 UITableViewCell 。我给了它 1000 标签,这样我就可以从我的 cellForRowAtIndexPath 访问它,我还用一些数据预加载了数组,比如

- (void)viewDidLoad {
[super viewDidLoad];
_data = [[NSMutableArray alloc] init];
[_data addObject:@"Hassan"];
[_data addObject:@"Malik"];
[_data addObject:@""];
[_data addObject:@"dsldjsd"];
[_data addObject:@""];
[_data addObject:@"Halsda"];
[_data addObject:@"Hjdaslk"];[_data addObject:@"Hdjskldj"];
[_data addObject:@""];
[_data addObject:@"dsalkdmsalkn"];
[_data addObject:@""];
[_data addObject:@"jdopecnl"];
[_data addObject:@"Hassan"];
[_data addObject:@"sdsdklsmlfmf"];
[_data addObject:@""];
[_data addObject:@""];[_data addObject:@"sdnsad"];
[_data addObject:@"nsadn"];
[_data addObject:@"dlsadn"];
[_data addObject:@"sdslakdn"];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
NSLog(@"%ld",(long)indexPath.row);
NSLog(@"%@",[_data objectAtIndex:indexPath.row]);
UITextField * field = (UITextField*)[cell viewWithTag:1000];
if([[_data objectAtIndex:indexPath.row] length]>0)
{
[field setText:[_data objectAtIndex:indexPath.row]];
}
return cell;
}

当 View 第一次运行时,文本字段会使用数组中的字符串进行初始化,但是当我向下滚动 TableView 然后向上滚动时,所有条目都搞砸了,数据在文本字段中随机排列。
下图是未滚动的uitableview场景 enter image description here

但是当我向下滚动然后向上滚动时,我的 tableView 的情况就像
enter image description here

我还制作了自定义 UItableViewCell 类并为 Textfield 制作了 iboutlet 但结果相同。请建议我最好的解决方案

最佳答案

- (UITableViewCell *)tableView:(UITableView *)tableView    cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
NSLog(@"%ld",(long)indexPath.row);
NSLog(@"%@",[_data objectAtIndex:indexPath.row]);
UITextField * field = (UITextField*)[cell viewWithTag:1000];
field.text = @"";
if([[_data objectAtIndex:indexPath.row] length]>0)
{
[field setText:[_data objectAtIndex:indexPath.row]];
}
return cell;
}

您必须每次重置文本字段文本。

关于ios - UiTextField 在 UtableView 中使用时会造成数据困惑。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36809465/

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