当我触摸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 :(得分: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;
}
}
}
}
答案 1 :(得分: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"];
}
}
答案 2 :(得分: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"];
}
}
希望这有帮助。
答案 3 :(得分:0)
在用户更改选择之前,使用- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
方法b'来调用它。返回新的indexPath
或nil
,以更改建议的选择。