gpt4 book ai didi

iOS-indexPath 总是返回 nil

转载 作者:行者123 更新时间:2023-11-29 01:05:09 25 4
gpt4 key购买 nike

我在触摸 TableView 时使用此方法获取 indexPath

-(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row==0) {
return nil;
}else{
return indexPath;
}
}

我想将一个 NSString 传输到第二个 viewController,所以我的代码是这样的

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSString *name =self.nameArray[indexPath.row];
[segue.destinationViewController navigationItem].title = name;
id theSegue = segue.destinationViewController;
[theSegue setValue:name forKey:@"showName"];
}

但 indexPath 始终为 nil,并且 -tableView:tableView willDeselectRowAtIndexPath indexPath 可能不会执行。

那我怎么才能在touch cell的时候得到正确的indexPath呢?谢谢。

最佳答案

试试下面的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// set Segue Identifier from your first viewcontroller to second viewController in stoaryboard
[self performSegueWithIdentifier:@"firstVCToSecondVCSegueIdentifier" sender:indexPath];
}

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"firstVCToSecondVCSegueIdentifier"])
{
if ([sender isKindOfClass:[NSIndexPath class]])
{
NSIndexPath *indexPath = (NSIndexPath *)sender;
if ([segue.destinationViewController isKindOfClass:[SecondVC class]]) {
SecondVC *aSecondVC = (SecondVC*)segue.destinationViewController;
NSString *name =self.nameArray[indexPath.row];
aSecondVC.title = name;
}
}
}
}

关于iOS-indexPath 总是返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36492761/

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