gpt4 book ai didi

ios - 如何使用 Seque 在 tableView 中找到点击按钮的 indexPath

转载 作者:可可西里 更新时间:2023-11-01 04:04:20 27 4
gpt4 key购买 nike

我有一个使用原型(prototype)单元格的 UITableView

单元格有一个 UIButton,它使用 Segue 显示 UIViewController(模态)。

我的问题是:如何将单元格的 IndexPath 传递给第二个 View Controller

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"showGamerDetail"]) {

//its not worked:
NSIndexPath *indexPath = [self._tableView indexPathForSelectedRow];
nDetailViewController *destinationViewController = segue.destinationViewController;
destinationViewController.selectedRowTitle = [gamers objectAtIndex:indexPath.row];

destinationViewController.indexPath = indexPath;
destinationViewController.delegate = self;
}

最佳答案

暂时忘记您正在使用 Storyboard,让我们尝试遵循最佳方法。

您有多种解决方案,但我认为其中只有一种是最佳的:委托(delegate)模式

首先,您应该扩展您的单元格,并使用委托(delegate)在用户按下按钮时返回单元格。然后,您应该使用 indexPathForCell 获取 indexPath

让我们看看方法:

按按钮位置的单元格

- (void)buttonClicked:(id)sender
CGPoint buttonPosition = [sender convertPoint:CGPointZero
toView:self.tableView];
NSIndexPath *tappedIP = [self.tableView indexPathForRowAtPoint:buttonPosition];

// When necessary
// UITableViewCell *clickedCell = [self.tableView cellForRowAtIndexPath:tappedIP];
}

上述解决方案当然实现起来最快,但从设计/架构的角度来看,它并不是最好的。此外,您获得了 indexPath 但您需要计算所有其他信息。这是一种很酷的方法,但不是最好的方法。

按钮 super View 上的循环单元格

// ContactListViewController.m

- (IBAction)emailContact:(id)sender {
YMContact *contact = [self contactFromContactButton:sender];
// present composer with `contact`...
}

- (YMContact *)contactFromContactButton:(UIView *)contactButton {
UIView *aSuperview = [contactButton superview];
while (![aSuperview isKindOfClass:[UITableViewCell class]]) {
aSuperview = [aSuperview superview];
}
YMContactCell *cell = (id) aSuperview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
return [[self fetchedResultsController] objectAtIndexPath:indexPath];
}

这种方式获取cell的性能不如前者,也不优雅。

按按钮标记的单元格

- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.theLabel.text = self.theData[indexPath.row];
cell.button.tag = indexPath.row;
[cell.button addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}

-(void)doSomething:(UIButton *) sender {
NSLog(@"%@",self.theData[sender.tag]);
//sender.tag will be equal to indexPath.row
}

绝对没有。使用标签似乎是一个很酷的解决方案,但控件的标签可用于其他原因,例如下一个响应者等。我不喜欢这种方法。

单元格设计模式

// YMContactCell.h
@protocol YMContactCellDelegate
- (void)contactCellEmailWasTapped:(YMContactCell*)cell;
@end

@interface YMContactCell
@property (weak, nonatomic) id<YMContactCellDelegate> delegate;
@end

// YMContactCell.m
- (IBAction)emailContact:(id)sender {
[self.delegate contactCellEmailWasTapped:self];
}

// ContactListViewController.m
- (void)contactCellEmailWasTapped:(YMContactCell*)cell;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
YMContact *contact = [[self fetchedResultsController] objectAtIndexPath:indexPath];
// present composer with `contact` ...
}

这是我最喜欢的解决方案。使用 delegationblocks 是一种非常好的方法,您可以传递所有您想要的参数。事实上,您可能希望直接发回所需的信息,而无需稍后进行计算。

享受 ;)

关于ios - 如何使用 Seque 在 tableView 中找到点击按钮的 indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23784630/

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