UIPICKER在表视图单元格详细信息文本标签上查看选择



正如我在标题中所写的,我正在尝试将选取器视图(由数组填充)上选择的值记到我单击以调出选取器视图的单元格的detailTextLabel。(选取器视图显示在操作表中)。

我尝试做的是设置两个可变数组:pickerDatatableData 。最初,pickerData数组包含选取器视图中显示的值,而tableData数组只有一个值。

调用以下方法时,我尝试将所选内容记录到 tableData 数组中,然后使用此值替换detailTextLabel上的原始值

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [tableData replaceObjectAtIndex:row withObject:[pickerData objectAtIndex:row]];
}

detailTextLabeltableData 数组填充,这发生在方法中:

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

使用代码行:

cell.detailTextLabel.text = [pickerData objectAtIndex:indexPath.row];

做错了什么?关于我应该如何以不同的方式做到这一点的任何建议?我只编程了一个月,我相信这有一个简单的解决方案。提前谢谢你!

您需要初始化表数据 NSMutableArray :self.tableData = [[[NSMutableArray alloc] init] autorelease];

 id)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
UITableViewCell *cell =[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0];
cell.detailTextLabel.text = [pickerData objectAtIndex:row];
    [self.tableData insertObject:[pickerData objectAtIndex:row] atIndex:index]
}

您说正在使用tableData来更新detailTextLabel,但是当您查看- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 时,您正在使用pickerData cell.detailTextLabel.text = [pickerData objectAtIndex:indexPath.row];这导致了问题。您应该使用cell.detailTextLabel.text = [tableData objectAtIndex:indexPath.row];并希望它能解决您的问题。

您可以在pickerView didSelectRow中访问 detailTextLabel,然后将选取器值设置为此label

UILabel *detailTextLabel=(UILabel*)[[tableView cellForRowAtIndexPath:indexPath] detailTextLabel ];
detailTextLabel.text=[pickerData objectAtIndex:row];

最新更新