【问题标题】:display the selected value from picker view to particularlabel of a cell of collection view将选择器视图中的选定值显示到集合视图单元格的特定标签
【发布时间】:2017-09-05 03:22:05
【问题描述】:

here when i define a cell ,,then in picker it did not recognize and always be 0. so the label always be empty.OR rather then when i update my collection view then all element(9 element)of cell has been updated with selected value.now what i do for select a particular cell's label value and where i am wrong ?

我有一个 collectionview 单元格,其中包含一个带有标签的标签和一个按钮。当我单击按钮时,选择器视图打开,我在选择行上选择了一个值。现在我想更新我的特定单元格的标签具有选定的值,但所有单元格的标签已更新。当我从选择器中选择一个值时,我会做什么来更新我的单个标签。

**- (UICollectionViewCell *)collectionView:
(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath

{
 UICollectionViewCell *cell ;
   static NSString *identifier = @"Cell";

 {


   cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier          forIndexPath:0];

     NSURL *urlImage = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIImageURL, [[infoArray objectAtIndex:indexPath.row] valueForKey:@"product_image"]]];

    RemoteImageView *productImageView = (UIImageView *)[cell viewWithTag:100];
    productImageView .imageURL = urlImage;

     UILabel *lblProductName = (UILabel *)[cell viewWithTag:101];
    [lblProductName setText:[[infoArray objectAtIndex:indexPath.row] valueForKey:@"product_name"]];


     NSArray *arrayProductQuantity =[[infoArray  objectAtIndex:indexPath.row] valueForKey:@"product_qty"];
    for (NSDictionary *dicQuantity in arrayProductQuantity)
     {

        NSLog(@"quantity_name %@",[dicQuantity    valueForKey:@"quantity_name"]);
        if([arrayProductQuantity objectAtIndex:0])
         {
            NSString*prodctPrice =[[infoArray objectAtIndex:indexPath.row] valueForKey:@"product_price"];
             float value = [prodctPrice intValue];
             NSString* qntyValue =[dicQuantity valueForKey:@"value"];
             float quntyValueInt=[qntyValue intValue];
        if([qntyValue isEqual:@"1000"])
          {
            value=(value/1000*quntyValueInt);
              float roundedValue = round(2.0f * value) / 2.0f;
              NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
              [formatter setMaximumFractionDigits:1];
              [formatter setRoundingMode: NSNumberFormatterRoundDown];

              NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];

              UILabel *lblProductprice = (UILabel *)[cell viewWithTag:102];
              [lblProductprice setText:numberString];

              UILabel *lblProductQuantity = (UILabel *)[cell viewWithTag:103];
              lblProductQuantity.text =  [dicQuantity valueForKey:@"quantity_name"]  ;

              UILabel *selectedCategory = (UILabel *)[cell viewWithTag:604];
              selectedCategory.text = lblRow.text;

          }
        else
            {
                value=ceil(value/1000*quntyValueInt);
                float roundedValue = round(2.0f * value) / 2.0f;
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                [formatter setMaximumFractionDigits:1];
                [formatter setRoundingMode: NSNumberFormatterRoundDown];

                NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];

                UILabel *lblProductprice = (UILabel *)[cell viewWithTag:102];
                [lblProductprice setText:numberString];


                lblProductQuantity = (UILabel *)[cell viewWithTag:103];
                lblProductQuantity.text =  [dicQuantity valueForKey:@"quantity_name"]  ;

            }

          }
}
     }

    return cell;

}     

         -(void)collectionView:(UICollectionView *)collectionView   didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {

    detailViewController* myvc1 = [self.storyboard 
    instantiateViewControllerWithIdentifier:@"detailViewController"];

     myvc1.cat_dict=[infoArray objectAtIndex:indexPath.row];
        myvc1.fromVC = @"product";
       [self.navigationController pushViewController:myvc1 animated:true];
       }
    #pragma mark picker for choose quntity

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

    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        return [array_picker count];
    }

**    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        NSLog(@"You selected this: %@", [array_picker objectAtIndex: row]);

        UILabel *selectedCategory = (UILabel *)[cell viewWithTag:604];
        selectedCategory.text = lblRow.text;


    } **

【问题讨论】:

  • 请更新您目前所做的代码
  • 您应该为同一单元格中的标签和按钮提供相同的标签。在 action 方法中,您将从 UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:index]; 方法获取单元格。当用户从选择器中选择一个值时,您应该更新单元格内的标签
  • @AravindBhuvanendran 在操作方法中我已经得到了单元格。但是我没有得到单元格 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger )component UILabel *selectedCategory = (UILabel *)[cell viewWithTag:604]; selectedCategory.text = lblRow.text;在这里我想获取单元格。所以我可以更新我的特定单元格的标签。
  • 您必须将单击的单元格对象的引用保持为全局并在选取器委托方法中使用它
  • @AravindBhuvanendran 对不起,我是 ios 的新手,请详细说明您的观点,因为我已经显示了我的代码,我在其中定义了全局单元格,但在选取器的委托方法单元格中变为 0,在这一行 UILabel * selectedCategory = (UILabel *)[cell viewWithTag:604];或者如果我使用 cell= [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:0];那么它会占用所有单元格而不是特定的单元格。

标签: ios objective-c uipickerview uicollectionviewcell


【解决方案1】:

添加这些属性

@property (nonatomic, assign) NSInteger selectedIndex; @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

在 cellForRowatIndexPath 方法中添加这些代码

cell.button.tag = indexPath.row; [cell.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

添加新方法按钮点击:

   - (IBAction)buttonClick:(UIButton *)sender{
self.selectedIndex = sender.tag;

// Show pickerview 

}

在里面添加这些代码(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

    SampleCollectionViewCell *cell = (SampleCollectionViewCell *) [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIndex inSection:0]]; // Assuming you have only single section
cell.label.text = lblRow.text;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多