【问题标题】:Returning wrong row index in UITableView didSelectRowAtIndexPath [duplicate]在 UITableView didSelectRowAtIndexPath 中返回错误的行索引 [重复]
【发布时间】:2014-09-06 13:29:15
【问题描述】:

我正在使用同伴代码并且 didSelectRowAtIndexPath 返回错误的值并因随机选择而崩溃。请建议。

查看控制器.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTbl;
@end

查看控制器.m

#import "ViewController.h"
#import "ViewCell.h"
#import "SecondViewController.h"

@interface ViewController ()
{
NSArray *tableCount;
}

@end

@implementation ViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
tableCount = [NSArray arrayWithObjects:@"",@"1",@"2",@"3",@"4",@"5",@"6", nil];
}


#pragma mark - Table Configuration 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
 }
 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
  return [tableCount count];
 }
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Identifier = @"My Identifier";

ViewCell *cell = (ViewCell *)[tableView dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"ViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

cell.myLabel.text = [tableCount objectAtIndex:indexPath.row];

return cell;

 }

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 150;
}

 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

[secondViewController printRowNumber:indexPath.row+1];

NSString *selectedRow = [tableCount objectAtIndex:indexPath.row+1];

secondViewController.selectedRow = selectedRow;

[self presentViewController:secondViewController animated:YES completion:^{
    [secondViewController printRowNumber:indexPath.row+1];
}];
}


 - (void)didReceiveMemoryWarning
 {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
 }

 @end

SecondView Controller.h

 #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 Controller.m

   #import "SecondViewController.h"

   @interface SecondViewController ()

    @end

    @implementation SecondViewController
    @synthesize mySecond;
    @synthesize selectedRow;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
    }

  - (void)viewDidLoad
    {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self printRowNumber:[selectedRow intValue]];
    }

    - (void)didReceiveMemoryWarning
    {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
     }
   - (IBAction)back:(id)sender {
 [self dismissViewControllerAnimated:YES completion:NULL];
    }

    -(void) printRowNumber:(int)num{
mySecond.text = selectedRow;
NSLog(@"%@",mySecond.text);
NSLog(@"%d",num);
     }

    @end

【问题讨论】:

  • @JoshCaswell 令人惊讶的是,由于这个小错字已经发布了多少问题。每当有人提到“didSelectRow ...”的 indexPath 问题时,我现在首先检查错误的“didDeselectRow ...”。
  • 请停止使用 Xcode 标签。和你的问题完全没有关系。
  • 我正在寻找一个解决方案,而不是废话 cmets 和建议,谢谢,坚持我不是你的端口,如果你对这个问题不感兴趣而不是戳,你可以继续前进。
  • @user3807171 我通过将您的问题标记为重复问题为您提供了解决方案。您使用了错误的方法。请参阅重复问题中接受的答案。这就是本网站的运作方式。
  • 感谢@rmaddy

标签: ios objective-c uitableview


【解决方案1】:

这是因为 IndexPath.Row + 1。 顺便说一句,您为什么要使用它?有什么好的理由吗? 这将导致崩溃伙伴。这就是正在发生的事情

数组索引从 0 开始,indexPath.row 也是如此。当您尝试调用 indexPath.row+1 时,它会让您崩溃并出现错误“索引超出数组范围”

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

【讨论】:

  • 如果我不使用 IndexPath.Row + 1。它会返回以前的值,所以我正在使用它
  • 您确定您使用的是 didDeselectRowAtIndexPath 方法而不是 didSelectRowAtIndexPath 吗?
  • 是的,我确定我正在使用 didDeselectRowAtIndexPath
猜你喜欢
  • 2014-12-22
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多