Ios 带UIButton的Segue TableViewCell
在我的tableview中有一个带有按钮的自定义位置单元格。当我按下按钮时,我想让它切换到该位置的啤酒细节。我使用的是故事板,我可以通过以下代码将整个单元连接到BeerTableViewController来实现Ios 带UIButton的Segue TableViewCell,ios,uitableview,uistoryboardsegue,Ios,Uitableview,Uistoryboardsegue,在我的tableview中有一个带有按钮的自定义位置单元格。当我按下按钮时,我想让它切换到该位置的啤酒细节。我使用的是故事板,我可以通过以下代码将整个单元连接到BeerTableViewController来实现 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if([[segue identifier] isEqualToString:@"beer"]){ NSIndexPath
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"beer"]){
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
BeerTableViewController *beer = (BeerTableViewController *)[segue destinationViewController];
Location *location = [self.location_results objectAtIndex:indexPath.row];
beer.location_id = location.location_id;
beer.location_name = location.location_name;
}
}
但是,当我从按钮而不是整个单元格创建一个分段时,无论单击哪一行,它总是与第一行分段。很明显,我不是按按钮通过这一排。我在这里研究了许多解决方案,但没有找到明确的答案。有什么想法吗
编辑1
我正在TableViewController中尝试以下代码
-(void)beerTapsPressed:(id)sender {
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
[self performSegueWithIdentifier:@"beer" sender:clickedButtonPath];
}
我用ctrl+将情节提要中的一个片段从TableView拖到DetailView,并将该片段命名为“beer”,我在单元格中有一个名为“beerTaps”的按钮,但无论单击哪一行,我仍然只能获得第1行的详细信息。您可以在TableView控制器和目标控制器之间连接该片段,然后,当有人点击按钮时,您可以调用performsguewithidentifier。至于传递点击按钮所在单元格的indexPath,您可以使用如下方式:
-(void)buttonPressed:(id)sender {
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
}
您还可以设置按钮的tag属性,以确定是哪一个按钮,但如果表中有多个分区,则会出现问题。如果您确实使用了该方法,您将需要确保在重新使用单元格时重置标记…您可以连接tableview控制器和目标控制器之间的分段,然后在有人点击按钮时调用PerformsgueWithIdentifier。至于传递点击按钮所在单元格的indexPath,您可以使用如下方式:
-(void)buttonPressed:(id)sender {
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
}
您还可以设置按钮的tag属性,以确定是哪一个按钮,但如果表中有多个分区,则会出现问题。如果您确实使用了该方法,则在重复使用该单元时,您需要确保重置标记…如果您将segue连接到按钮上,则该方法应该可以工作:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"beer"]) {
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
BeerTableViewController *beer = (BeerTableViewController *)[segue destinationViewController];
Location *location = [self.location_results objectAtIndex:clickedButtonPath.row];
beer.location_id = location.location_id;
beer.location_name = location.location_name;
}
}
不过,通常最好不要使用按钮在视图层次结构中的位置来获取单元格。更好的方法是在cellForRowAtIndexPath中为按钮提供一个等于indexPath.row的标记。然后在prepareForSegue中,您可以执行以下操作:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender {
if([[segue identifier] isEqualToString:@"beer"]) {
BeerTableViewController *beer = (BeerTableViewController *)[segue destinationViewController];
Location *location = [self.location_results objectAtIndex:sender.tag];
beer.location_id = location.location_id;
beer.location_name = location.location_name;
}
}
如果您将segue连接到按钮上,则此功能应能正常工作:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"beer"]) {
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
BeerTableViewController *beer = (BeerTableViewController *)[segue destinationViewController];
Location *location = [self.location_results objectAtIndex:clickedButtonPath.row];
beer.location_id = location.location_id;
beer.location_name = location.location_name;
}
}
不过,通常最好不要使用按钮在视图层次结构中的位置来获取单元格。更好的方法是在cellForRowAtIndexPath中为按钮提供一个等于indexPath.row的标记。然后在prepareForSegue中,您可以执行以下操作:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender {
if([[segue identifier] isEqualToString:@"beer"]) {
BeerTableViewController *beer = (BeerTableViewController *)[segue destinationViewController];
Location *location = [self.location_results objectAtIndex:sender.tag];
beer.location_id = location.location_id;
beer.location_name = location.location_name;
}
}
谢谢,我正在尝试你的解决方案,但仍然有不足之处。我编辑了我的问题以反映我的尝试。有什么想法吗?这对我很有用。在我的例子中,我必须像[[[sender superview]superview]superview]一样使用它,因为我在UITableViewCell的contentView中有另一个UIView层。谢谢,我正在尝试您的解决方案,但仍然有不足之处。我编辑了我的问题以反映我的尝试。有什么想法吗?这对我很有用。在我的例子中,我不得不像[[[sender superview]superview]superview]那样使用它,因为我在UITableViewCell的contentView中有另一个UIView层。我最后给了按钮一个标签并使用了您建议的方法。谢谢最后我给了按钮一个标签,并使用了你建议的方法。谢谢