0

在我的应用程序中,我实现了如下功能,Tabelview 单元格包含两个单击按钮,它显示选择器视图,当我选择选择器值时,它不会反映在 uitabelview 对应的 cell.label 上,这里是代码。

表格视图单元格

    if(tableView.tag==25)
    {
        static NSString *simpleTableIdentifier = @"ordercel";
        ordercel *cell = (ordercel *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ordercel" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        cell.pimg.image=[arr objectAtIndex:indexPath.row];
        cell.sizlb.text=@"45";
        cell.qtylb.text=[qtyary objectAtIndex:indexPath.row];
        [cell.contentView addSubview:lbl1];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = indexPath.row+2000;
        [button addTarget:self action:@selector(BtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor=[UIColor clearColor];
        button.frame = CGRectMake(130, 20 , 70, 35);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cell addSubview:button];

        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        button1.tag = indexPath.row+4000;
        [button1 addTarget:self action:@selector(sizPressed:) forControlEvents:UIControlEventTouchUpInside];
        button1.backgroundColor=[UIColor clearColor];
        button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button1.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        button1.frame = CGRectMake(230, 20 , 70, 35);
        [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cell addSubview:button1];
        return cell;
    }

按钮方法

-(void)BtnPressed:(UIButton*)sender
{
    [self createActionSheet];
    pickerType = @"qtypicker";
    UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
    chPicker.tag=sender.tag-2000;
    chPicker.dataSource = self;
    chPicker.delegate = self;
    chPicker.showsSelectionIndicator = YES;
    [actionSheet addSubview:chPicker];
}

选择器查看代码

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

    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
    if ([pickerType isEqualToString:@"qtypicker"])
    {
        counti = [array count];
    }
    if ([pickerType isEqualToString:@"sizepicker"])
    {
        counti = [array1 count];
    }
    return counti;
}

    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *string;
    if ([pickerType isEqualToString:@"qtypicker"])
    {
        string = [array objectAtIndex:row];
    }
    if ([pickerType isEqualToString:@"sizepicker"])
    {
        string = [array1 objectAtIndex:row];
    }
    return string;
    }
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component      {
    return 300;
    }

    - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    UITableViewCell *owningCell = (UITableViewCell*)[thePickerView superview];
    NSIndexPath *pathToCell = [tabvu indexPathForCell:owningCell];
    int index1=thePickerView.tag;//= thePickerView.tag-2000;
    if ([pickerType isEqualToString:@"qtypicker"])
    {
         UILabel *obj=((UILabel *)[self.view viewWithTag:index1+1000]);
         obj.text = [array objectAtIndex:row];
        [qtyary addObject:obj.text];
        [tabvu reloadData];
    }
    if ([pickerType isEqualToString:@"sizepicker"])
    {

    }
    }

如何在选定的单元格上获取选定的选择器值。我应该在我的代码中进行哪些更改?请帮我整理一下

4

2 回答 2

0

您的代码很难阅读。但这应该是方式。

pickerView:(UIPickerView *)thePickerView didSelectRow 

方法更新数组对象[qtyary objectAtIndex:indexPath.row]。然后调用[tableView reloadData];这将更新您的cell.qtylb.text. 对于其他标签,您还需要一个将更新的数组。我希望这行得通。

于 2013-05-22T05:07:08.343 回答
0

尝试在 didSelectRow 中更改行

UITableViewCell *owningCell = (UITableViewCell*)[thePickerView superview];  

ordercel *owningCell = (ordercel*)[thePickerView superview];

如果您有用于放置按钮的自定义单元格。那么您应该使用自定义单元格的单元格实例而不是 UITableViewCell。

并尝试在这种情况下打印日志

    if ([pickerType isEqualToString:@"qtypicker"])
{
     UILabel *obj=((UILabel *)[self.view viewWithTag:index1+1000]);
     obj.text = [array objectAtIndex:row];
    [qtyary addObject:obj.text];
    [tabvu reloadData];
}

它返回什么对象。将 NSLog 应用于 obj。

于 2013-05-22T05:29:27.663 回答