【问题标题】:How to get UIPickerView value in UITableView如何在 UITableView 中获取 UIPickerView 值
【发布时间】:2014-01-30 06:33:28
【问题描述】:

我的 UITableViewController 的每一行都有一个 UIPickerView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 CartCell *cell = (CartCell *)[tableView dequeueReusableCellWithIdentifier:@"CartCell"];
    Cart *cart = (self.carts)[indexPath.row];
   cell.productnameLabel.text = cart.productname;
   cell.priceLabel.text = cart.price;

 picker = [[UIPickerView alloc]initWithFrame:CGRectMake(70, 0, 100, 40)];
    [picker setDelegate:self];

    arrayColors = [[NSMutableArray alloc] init];
[arrayColors addObject:@"Red"];
[arrayColors addObject:@"Orange"];
[arrayColors addObject:@"Yellow"];
[arrayColors addObject:@"Green"];
[arrayColors addObject:@"Blue"];
[arrayColors addObject:@"Indigo"];
[arrayColors addObject:@"Violet"];

    [cell addSubview:picker];

return cell;
}

我有选择器的 didSelectRow

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    NSLog(@"Selected Color: %@. Index of selected color: %i", [arrayColors objectAtIndex:row], row);
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
         NSLog(@"indexPathForSelectedRow  %@", path);

}

但我将路径值设为空。 如何在 UITableView 中获取 UIPickerView 值。我需要每一行的选择器值。

【问题讨论】:

  • 你想要什么,为什么要创建每个单元格 UIPickerView 和 NSMutableArray ?
  • 我在这张表中列出了购物车。我想更新购物车中产品的数量(或颜色)。

标签: objective-c ios7 uitableview uipickerview


【解决方案1】:

试试这个...

UITableViewCell *cell = (UITableViewCell *)[thePickerView superview];
NSIndexPath *path = [self.tableView indexPathForCell:cell];
NSLog(@"indexPathForSelectedRow  %@", path);

【讨论】:

  • indexPathForCell 也返回空值。是因为自定义单元格吗?
  • 很抱歉我没有提到我的 sdk 版本。它是ios。我从这个链接stackoverflow.com/questions/18743099/… 得到了解决方案
  • 和修改代码 - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { UIView *view = thePickerView; while (view != nil && ![view isKindOfClass:[UITableViewCell class]]) { view = [view superview]; } UITableViewCell *cell = (UITableViewCell *)view; NSIndexPath *path = [self.tableView indexPathForCell:cell]; NSLog(@"indexPathForSelectedRow %@", 路径); NSLog(@"section 为 %d,row 为 %d", path.section, path.row); }
  • hmn... 一切都很好,如果您使用 CartCell 代替 UITableViewCell 会更好。排队 while (view != nil && ![view isKindOfClass:[UITableViewCell class]])
  • 我试过 CartCell *cell = (CartCell *)[thePickerView superview];但同样的空结果来了。但它适用于 UIView while 循环。不知道为什么它不能与 CartCell 一起使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多