如何通过单击UITableViewCell的按钮发送数据到另一个ViewController

时间:2018-02-22 07:35:01

标签: ios objective-c json uitableview navigation

我想使用uitableviewcell中的按钮点击发送数据。我从Web服务获取数据。每行按钮向viewController发送不同的数据。  附上下面的cellForRowAtIndex方法:

-(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;
}

按钮的方法如下:

-(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];
//}
}

请帮帮我 提前致谢

2 个答案:

答案 0 :(得分:1)

您需要获取所选单元格按钮的索引。你可以从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];

}

cell.btnShowRatingDetails.tag = indexPath.row;按钮分配标记后,您可以在操作方法中获取数据对象,与使用indexpath.row获取数据对象的方式相同。

答案 1 :(得分:0)

您需要在NSDictionary *dictReview = [_arrHandMadeList objectAtIndex:sender.tag];中使用-(void) showRatingClicked:(UIButton*)sender来获取相应的词典,然后在dictMenuData的位置使用它。

从您的代码中完全删除dictMenuData