gpt4 book ai didi

ios - UITableView 附件按钮 Segue 不工作

转载 作者:行者123 更新时间:2023-11-28 18:06:03 26 4
gpt4 key购买 nike

我在学习如何使用 segues 将数据从单元格附件按钮传递到详细 View Controller 时遇到了很大的问题。如果我点击单元格,数据将传递到 NSString *selectedItem; 中的详细 View Controller ,并且另一个 View 上的标签会更新为单元格中的单词 -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"toDetail" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"toDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}

然后我将 Storyboard上的一个 segue 从单元格连接到详细 View ,名称为“toDeatil” 当我单击单元格时它会工作,它会转到详细信息屏幕,但如果我把

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"toDetail" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"toDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}

几乎完全相同,但是从 accessorybutton 开始,我将 Storyboard上的一个 segue 从单元格 accessorybutton 连接到名为“toDetail< 的详细 View ” 当我单击单元格时,它会转到详细信息屏幕,但信息不存在,标签 dosent 被填写或一直使用第一个单元格详细信息。

我试过很多组合。我试过在自定义单元格中制作我自己的附件按钮,一个 UIButton,但它始终只使用第一个单元格的详细信息。

目标是让 didSelectRowAtIndexPath 转到一个 View ,让 accessoryButtonTappedForRowWithIndexPath 转到另一个 View ,但我需要告诉 accessoryButtonTappedForRowWithIndexPath 单元格是什么按下。

希望这是有道理的。

谢谢

最佳答案

您可以将 indexPath 作为 sender 传递给 performSegueWithIdentifier 调用,如下所示:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"toDetail" sender:indexPath];//IndexPath as sender
}

在您的prepareForSegue 方法中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"toDetail"]) {
//Detect sender class and act accordingly
NSIndexPath *indexPath = [sender isKindOfClass:[NSIndexPath class]] ? (NSIndexPath*)sender : [self.tableView indexPathForSelectedRow];
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}

关于ios - UITableView 附件按钮 Segue 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18738658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com