dequeueReusableCellWithIdentifier 改变多个 UIPickerviews 的值
dequeueReusableCellWithIdentifier change multiple UIPickerviews Value
我正在创建一个具有 pickerView 的自定义单元格,当我 运行 应用程序加载所有具有选择器视图的单元格时。一次可以看到 4 个单元格,当我更改第一个选择器视图的值并向下滚动时。每第 4 个选择器的值已更改。我得到了所有选择器视图的值,它 returns 正确的值,意味着它只更改第一个选择器视图的值。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AddTaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addTaskCell" forIndexPath:indexPath];
AddTaskDetails *task =[self.tasksArray objectAtIndex:indexPath.row];
NSAttributedString *attributedString = [self attributedTextForTask:task.taskDetail];
cell.taskDetails.attributedText = attributedString;
cell.hourePicker.tag = indexPath.row;
[cell.hourePicker selectRow:0 inComponent:0 animated:YES];
cell.addDescriptionBtn.tag = indexPath.row;
[cell.addDescriptionBtn addTarget:self action:@selector(didTapAddDescButton:) forControlEvents:UIControlEventTouchUpInside];
return cell; }
发生这种情况是因为正在重新使用单元格,但从未重置选择器。
配置要显示的单元格时,需要先将选取器设置为默认值,然后再将其设置为该单元格的正确值(如果适用)。
您需要将选择器中选择的值存储在 table 单元格以外的其他位置。 table 单元格将被重新使用,因此您需要一个数据结构(大概是您的数据源)。
为
中的每个单元格设置选择器视图值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
如果您不在此处为每个单元格设置,选择器视图值将会很奇怪。它在滚动后显示相同值的原因是因为 iOS 只是拾取可见单元格并在滚动时再次显示它们。因此,除非您在 cellForRowAtIndexPath:
中设置值,否则它们将具有以前的值。
在浪费了 6 个小时后,我解决了问题,这是更新后的代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AddTaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addTaskCell" forIndexPath:indexPath];
AddTaskDetails *task =[self.tasksArray objectAtIndex:indexPath.row];
NSAttributedString *attributedString = [self attributedTextForTask:task.taskDetail];
cell.taskDetails.attributedText = attributedString;
cell.hourePicker.tag = indexPath.row;
cell.addDescriptionBtn.tag = indexPath.row;
[cell.addDescriptionBtn addTarget:self action:@selector(didTapAddDescButton:) forControlEvents:UIControlEventTouchUpInside];
AddTaskDetails *task_1 =[self.tasksArray objectAtIndex:indexPath.row];
if(task_1.hours>0)
{
[cell.hourePicker selectRow:task_1.hours inComponent:0 animated:NO];
}
else
{
[cell.hourePicker selectRow:0 inComponent:0 animated:NO];
}
return cell;
}
我正在创建一个具有 pickerView 的自定义单元格,当我 运行 应用程序加载所有具有选择器视图的单元格时。一次可以看到 4 个单元格,当我更改第一个选择器视图的值并向下滚动时。每第 4 个选择器的值已更改。我得到了所有选择器视图的值,它 returns 正确的值,意味着它只更改第一个选择器视图的值。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AddTaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addTaskCell" forIndexPath:indexPath];
AddTaskDetails *task =[self.tasksArray objectAtIndex:indexPath.row];
NSAttributedString *attributedString = [self attributedTextForTask:task.taskDetail];
cell.taskDetails.attributedText = attributedString;
cell.hourePicker.tag = indexPath.row;
[cell.hourePicker selectRow:0 inComponent:0 animated:YES];
cell.addDescriptionBtn.tag = indexPath.row;
[cell.addDescriptionBtn addTarget:self action:@selector(didTapAddDescButton:) forControlEvents:UIControlEventTouchUpInside];
return cell; }
发生这种情况是因为正在重新使用单元格,但从未重置选择器。
配置要显示的单元格时,需要先将选取器设置为默认值,然后再将其设置为该单元格的正确值(如果适用)。
您需要将选择器中选择的值存储在 table 单元格以外的其他位置。 table 单元格将被重新使用,因此您需要一个数据结构(大概是您的数据源)。
为
中的每个单元格设置选择器视图值-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
如果您不在此处为每个单元格设置,选择器视图值将会很奇怪。它在滚动后显示相同值的原因是因为 iOS 只是拾取可见单元格并在滚动时再次显示它们。因此,除非您在 cellForRowAtIndexPath:
中设置值,否则它们将具有以前的值。
在浪费了 6 个小时后,我解决了问题,这是更新后的代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AddTaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addTaskCell" forIndexPath:indexPath];
AddTaskDetails *task =[self.tasksArray objectAtIndex:indexPath.row];
NSAttributedString *attributedString = [self attributedTextForTask:task.taskDetail];
cell.taskDetails.attributedText = attributedString;
cell.hourePicker.tag = indexPath.row;
cell.addDescriptionBtn.tag = indexPath.row;
[cell.addDescriptionBtn addTarget:self action:@selector(didTapAddDescButton:) forControlEvents:UIControlEventTouchUpInside];
AddTaskDetails *task_1 =[self.tasksArray objectAtIndex:indexPath.row];
if(task_1.hours>0)
{
[cell.hourePicker selectRow:task_1.hours inComponent:0 animated:NO];
}
else
{
[cell.hourePicker selectRow:0 inComponent:0 animated:NO];
}
return cell;
}