2016-04-08 40 views
0

我用這個方法得到indexPath當我觸摸到的TableViewIOS的​​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始終爲零,並且-tableView:tableView willDeselectRowAtIndexPath indexPath可能不會執行。

所以當我觸摸單元格時,如何才能獲得正確的indexPath? 謝謝。

+0

表視圖從0開始,行你確定要忽略第一行? – Nick

+0

是的,第一個只是一個例子。 – Shucheng

+0

您需要使用tableview委託didSelectRowAtIndextPath來獲取選定的行信息。別忘了設置你的tableview委託。 – Nick

回答

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; 
      } 
     } 
    } 
} 
+0

它對我很有幫助,非常感謝。但我仍不明白爲什麼我無法得到正確的indexPath。 – Shucheng

+0

爲什麼你會在willDeselectRowAtIndexPath委託方法中返回nil? –

0

嘗試這種::

-(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"]; 
     } 
    } 
+0

是的,它的工作。但我想在' - (void)prepareForSegue:(UIStoryboardSegue *)中使用這個數字segue sender:(id)sender'所以我想宣佈另一個NSInteger? – Shucheng

+0

創建nsinterger,併爲nsinteger分配單元標籤 –

0

試試這個

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

希望這有助於。

+0

是的,indexPath不是零,但'indexPath'.row總是0.indexPath總是0xc000000000000016 – Shucheng

0

使用- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath方法b'coz它在用戶更改選擇之前調用。返回新的indexPathnil,更改建議的選擇。