【问题标题】:iOS-indexPath always return niliOS-indexPath 总是返回 nil
【发布时间】:2016-04-08 06:13:19
【问题描述】:

当我触摸 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 可能不会执行。

那么当我触摸单元格时如何获得正确的 indexPath? 谢谢。

【问题讨论】:

  • 表格视图从第 0 行开始。确定要忽略第一行吗?
  • 是的,第一个只是一个例子。
  • 您需要使用 tableview 委托 didSelectRowAtIndextPath 来获取选定的行信息。并且不要忘记设置您的 tableview 委托。
  • 您想在 didSelectRow 或 didDeselectRow 上获取 indexPath??

标签: ios objective-c iphone uitableview


【解决方案1】:

试试下面的代码

- (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;
            }
        }
    }
}

【讨论】:

  • 它对我有用,非常感谢。但我仍然不明白为什么我无法获得正确的 indexPath。
  • 为什么在 willDeselectRowAtIndexPath 委托方法中返回 nil ?
【解决方案2】:

试试这个

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     [self performSegueWithIdentifier:@"YOUR_IDENTIFIER" sender:nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
        if (indexPath != nil)
        {
     NSString *name =self.nameArray[indexPath.row];
    [segue.destinationViewController navigationItem].title = name;
    id theSegue = segue.destinationViewController;
    [theSegue setValue:name forKey:@"showName"];
        }
    }

希望这会有所帮助。

【讨论】:

  • 是的,indexPath 不是 nil,但是 indexPath.row 总是 0.indexPath 总是 0xc000000000000016
【解决方案3】:

使用- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 方法,因为它在用户更改选择之前调用。返回一个新的indexPathnil,以更改建议的选择。

【讨论】:

    【解决方案4】:

    试试这个::

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
     {
           [self performSegueWithIdentifier:@"YOUR_IDENTIFIER" sender:nil];
    
       nslog(@"tag :: %@", indexpath.row);
    }
    
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
            NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
            if (indexPath != nil)
            {
         NSString *name =self.nameArray[indexPath.row];
        [segue.destinationViewController navigationItem].title = name;
        id theSegue = segue.destinationViewController;
        [theSegue setValue:name forKey:@"showName"];
            }
        }
    

    【讨论】:

    • 是的,它成功了。但是我想在-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 中使用这个号码所以我想宣布另一个 NSInteger?
    • 创建nsinterger,并将cell标签赋给nsinteger
    猜你喜欢
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 2016-08-25
    • 2017-02-10
    • 1970-01-01
    • 2017-03-28
    • 2015-03-05
    • 2014-08-25
    相关资源
    最近更新 更多