2014-01-30 17 views
0

得到UIPickerView值我有我的UITableViewController如何在UITableView的

- (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; 
} 

的每行一個UIPickerView和我有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); 

} 

但我得到的路徑vaue爲空值。 如何在UITableView中獲取UIPickerView值。我需要每行的選擇器值。

+0

你想要什麼,爲什麼你創建的每個細胞UIPickerView和NSMutableArray裏? –

+0

我在此表中列出購物車。我想更新購物車中產品的數量(或顏色)。 – Vinod

回答

1

嘗試......

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

也返回indexPathForCell的空值。是因爲自定義單元嗎? – Vinod

+0

對不起,我沒有提到我的SDK版本。它是ios。我從這個鏈接得到了解決方案http://stackoverflow.com/questions/18743099/indexpathforcell-returns-nil-since-ios7 – Vinod

+0

和修改後的代碼 - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)組件{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%@」,path); NSLog(@「節是%d,行是%d」,path.section,path.row); } – Vinod