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.
**- (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
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
SampleCollectionViewCell *cell = (SampleCollectionViewCell *) [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIndex inSection:0]]; // Assuming you have only single section
cell.label.text = lblRow.text;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.