【问题标题】:How to send data by clicking UITableViewCell's button to another ViewController如何通过单击 UITableViewCell 按钮将数据发送到另一个 ViewController
【发布时间】:2018-02-22 07:35:01
【问题描述】:

我想使用 uitableviewcell 中的按钮单击发送数据。我从网络服务获取数据。每个行按钮向 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];
//}
}

请帮帮我 提前致谢

【问题讨论】:

标签: ios objective-c json uitableview navigation


【解决方案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 获取数据对象的方式相同。

【讨论】:

    【解决方案2】:

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

    从您的代码中完全删除 dictMenuData

    【讨论】:

    • 很高兴它有帮助。 :)
    猜你喜欢
    • 2023-03-31
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    相关资源
    最近更新 更多