[英]display the selected value from picker view to particularlabel of a cell of collection view
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 ? 在这里,当我定义一个单元格时,然后在选择器中它无法识别并且始终为0。因此标签始终为空。或者,而不是当我更新集合视图时,单元格的所有element(9 element)都已更新为selected value.now我要如何选择特定单元格的标签值?我在哪里错了?
I have a collectionview cell which contains a label with tag and a button.when i click the button a picker view open and I select a value on did select row.Now i want to update my particular cell' label with selected value,but label of all cell has been updated.what i do for update my single label when i select a value from picker.
我有一个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;
} **
Add these property 添加这些属性
@property (nonatomic, assign) NSInteger selectedIndex; @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
In cellForRowatIndexPath method add these codes 在cellForRowatIndexPath方法中添加以下代码
cell.button.tag = indexPath.row; [cell.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
Add new method buttonClick: 添加新方法按钮单击:
- (IBAction)buttonClick:(UIButton *)sender{
self.selectedIndex = sender.tag;
// Show pickerview
} }
Inside add these code (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
在内部添加这些代码
(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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.