iphone - UITableViewCell 中的 UIPickerView :issue in showing the value at particular cell

标签 iphone uipickerview

我正在使用 UITableView 中的 UIPickerView 来选择整数值。我不知道如何将值设置为特定单元格中的标签。下面是我的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
cell = [self.mTicketTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell.bg.png"]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

lblType = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 90, 30)];
lblPrice = [[UILabel alloc]initWithFrame:CGRectMake(140, 15, 90, 30)];
lblSeat = [[UILabel alloc]initWithFrame:CGRectMake(232, 21, 52, 26)];
lblSeat.text = strSelected;
lblSeat.tag = indexPath.row+1;
lblSeat.textColor = [UIColor lightGrayColor];
lblSeat.textAlignment = NSTextAlignmentCenter;
lblSeat.backgroundColor = [UIColor clearColor];
UIButton *btnPicker = [UIButton buttonWithType:UIButtonTypeCustom];
[btnPicker setImage:[UIImage imageNamed:@"drop_down.png"] forState:UIControlStateNormal];
[btnPicker addTarget:self action:@selector(pickseats:) forControlEvents:UIControlEventTouchUpInside];
btnPicker.frame = CGRectMake(232, 20, 80, 30);

self.mPickerView = [[UIPickerView alloc] init];
self.mPickerView.frame = CGRectMake(228, 100, 90, 60); 
self.mPickerView.showsSelectionIndicator = YES;
self.mPickerView.delegate = self;
self.mPickerView.dataSource = self;
[self.mPickerView setNeedsLayout];
self.mPickerView.transform = CGAffineTransformMakeScale(0.75f, 0.75f);
self.mPickerView.hidden = TRUE;
[self.view addSubview:self.mPickerView];

lblType.text = [arrType objectAtIndex:indexPath.row];
if([arrCost count] == 0)
{
    lblPrice.text = @"0";
}
else{
lblPrice.text = [arrCost objectAtIndex:indexPath.row];
}
[cell addSubview:lblType];
[cell addSubview:lblPrice];
[cell addSubview:btnPicker];
[cell addSubview:lblSeat];

return cell;
}

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 10;
}

-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [numbers objectAtIndex:row];
}

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
strSelected = [numbers objectAtIndex:row];
NSLog(@"str selection:%@", strSelected);
self.mPickerView.hidden = TRUE;
[self.mTicketTable reloadData];
}

请指导以上内容。

提前致谢。

最佳答案

根据我的说法,你可以做的是:

首先将标签分配给您的 btnPicker 和 lblSeat

btnPicker.tag = indexPath.row;
lblSeat.tag = 11111;

然后在 pickerView 的 didSelectRow 方法中将选定的值分配给您的数组。

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    int index = pickerView.tag;
    strSelected = [numbers objectAtIndex:row];
    NSIndexPath *path = [NSIndexPath indexPathForRow:index inSection:0];

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];

    for (UIView* tempView in [cell subviews]) {
        if ([tempView isKindOfClass:[UILabel class]]) {
            if (tempView.tag == 11111) {
                UILabel *lblTemp = (UILabel *)tempView;
                lblTemp.text = strSelected;
                break;
            }
        }
    }

    self.mPickerView.hidden = TRUE;
}

更换选座方法:

-(void)pickseats:(id)sender
{
    UIButton *btn = (UIButton *) sender;
    self.mPickerView.tag = btn.tag;
    self.mPickerView.hidden = FALSE;
}

我认为这会起作用。如果不是请告诉我。 谢谢。

关于iphone - UITableViewCell 中的 UIPickerView :issue in showing the value at particular cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16544321/

相关文章:

javascript - iphone/ipad 大 Canvas vs 小 Canvas + div 动画

iOS 选择器 View 作为默认输入法

ios - Xcode:在部署信息下指定 iPhone 和 iPad 的方向?

ios - 从 NSString 中删除单词

iPhone 剪辑图像与路径

iOS - 显示在 Storyboard 而不是键盘中制作的 UIPIckerView

ios - 将从 UIPicker 中选择的值分配给 Swift 中的 UITextField(以及如何显示和隐藏它)

ios - 我们如何在选择段后调用 PickView 的 didSelectRow?

ios - 多个选择器 View : unexpectedly found nil while unwrapping an Optional value

iphone - UIImageview 上的 UIRotationGestureRecognizer