Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITableView中返回错误的行索引didSelectRowAtIndexPath_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 在UITableView中返回错误的行索引didSelectRowAtIndexPath

Ios 在UITableView中返回错误的行索引didSelectRowAtIndexPath,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在使用Fellow代码,didSelectRowAtIndexPath返回了错误的值,并通过随机选择崩溃。Pl建议 视图控制器.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *myTbl;

我正在使用Fellow代码,didSelectRowAtIndexPath返回了错误的值,并通过随机选择崩溃。Pl建议

视图控制器.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTbl;
@end
 #import <UIKit/UIKit.h>
 @interface SecondViewController : UIViewController
 {
 NSString *selectedRow;
  }
  @property (weak, nonatomic) IBOutlet UILabel *mySecond;
  @property (nonatomic,retain) NSString *selectedRow;
  @property (weak, nonatomic) IBOutlet UIButton *back;
 - (IBAction)back:(id)sender;

  -(void) printRowNumber:(int)num;
   @end
SecondView控制器.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTbl;
@end
 #import <UIKit/UIKit.h>
 @interface SecondViewController : UIViewController
 {
 NSString *selectedRow;
  }
  @property (weak, nonatomic) IBOutlet UILabel *mySecond;
  @property (nonatomic,retain) NSString *selectedRow;
  @property (weak, nonatomic) IBOutlet UIButton *back;
 - (IBAction)back:(id)sender;

  -(void) printRowNumber:(int)num;
   @end

这是因为indexath.Row+1。 顺便问一下,你为什么要用它?有什么好的理由吗? 这会导致车祸,伙计。这就是发生的事情

数组索引从0开始,indexath.row也是如此。当您尝试调用indexPath.row+1时,它将导致崩溃,错误为“索引超出了数组的范围”


在didSelectRowAtIndexPath中,将每个indexPath.row+1更改为indexPath.row。你的问题解决了。

@JoshCaswell由于这个小错误,有这么多问题被贴出来,真是令人惊讶。每当有人提到“didSelectRow…”的indexPath问题时,我现在首先检查是否有错误的“didSelectRow…”。请停止使用Xcode标记。这与你的问题毫无关系。我只是在寻找一个解决方案,而不是垃圾评论和建议。谢谢,坚持我不是你的职责,如果你对这个问题不感兴趣,你可以继续,而不是戳。@user3807171我通过将你的问题标记为重复问题来为你提供解决方案。你使用了错误的方法。请参见重复问题中的已接受答案。这就是这个网站的工作原理。感谢@rmaddyif我不使用IndexPath.Row+1。它返回上一个值,所以我使用它。你确定你使用的是DidDescelectRowatindexpath方法而不是didSelectRowAtIndexPath吗?是的,我确定我使用的是DidDescelectRowatindexpath