2

I want to send data using a button click which is in uitableviewcell. I got the data from web service. Each row button send different data to viewController. Attach cellForRowAtIndex Method below:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {

static NSString *identifier = @"CategoryCell";
NSDictionary *dictMenuData = [_arrHandMadeList objectAtIndex:indexPath.row];
CategoryCell *cell = (CategoryCell *)[tableView dequeueReusableCellWithIdentifier:identifier];

if(!cell) {
    [tableView registerNib:[UINib nibWithNibName:@"CategoryCell" bundle:nil] forCellReuseIdentifier:identifier];
    cell = (CategoryCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
}

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

[arrReview removeAllObjects];
//arrReview = [dictMenuData objectForKey:@"RivewData"];
[arrReview addObjectsFromArray:[dictMenuData objectForKey:@"RivewData"]];

[cell setupHandMadeCell:dictMenuData];

return cell;
}

Method for button is below:

-(void) showRatingClicked:(UIButton*)sender {

//if (sender.tag == currentIndex) {
ReviewVC *rvc = (ReviewVC*)[self.storyboard instantiateViewControllerWithIdentifier:@"ReviewVC"];

rvc.arrReviewDetails = arrReview;
rvc.strRestaurentName = [dictMenuData objectForKey:@"Restaurant_Name"];
rvc.strRestaurentId = [dictMenuData objectForKey:@"Restaurant_Id"];
rvc.strRestaurentRating = [dictMenuData objectForKey:@"Stars"];

[self.navigationController pushViewController:rvc animated:YES];
//}
}

Please help me Thanks in advance

Anupam Das
  • 67
  • 10

2 Answers2

1

You need to get the index of selected cell button. You can get it from sender.tag.

-(void) showRatingClicked:(UIButton*)sender {

ReviewVC *rvc = (ReviewVC*)[self.storyboard instantiateViewControllerWithIdentifier:@"ReviewVC"];

NSDictionary *dictMenuData = [_arrHandMadeList objectAtIndex:sender.tag];

rvc.arrReviewDetails = [dictMenuData objectForKey:@"RivewData"]; //or as per your data
rvc.strRestaurentName = [dictMenuData objectForKey:@"Restaurant_Name"];
rvc.strRestaurentId = [dictMenuData objectForKey:@"Restaurant_Id"];
rvc.strRestaurentRating = [dictMenuData objectForKey:@"Stars"];

[self.navigationController pushViewController:rvc animated:YES];

}

Once cell.btnShowRatingDetails.tag = indexPath.row; button is assigned the tag you can get the data object in the action method, same way you get it using indexpath.row.

Saad Chaudhry
  • 1,392
  • 23
  • 37
0

You need to use NSDictionary *dictReview = [_arrHandMadeList objectAtIndex:sender.tag]; in -(void) showRatingClicked:(UIButton*)sender to get the appropriate dictionary, then use it at the place of dictMenuData.

Remove dictMenuData altogether from your code.

Yogi
  • 3,578
  • 3
  • 35
  • 56