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

时间:2014-07-16 06:30:07

标签: ios objective-c uitableview

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

查看Controller.h

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

查看Controller.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

1 个答案:

答案 0 :(得分:1)

因为IndexPath.Row + 1。 为什么你顺便使用它?有什么好理由? 这将导致崩溃的伙伴。这就是发生的事情

数组索引从0开始,因此是indexPath.row。当你试图调用indexPath.row + 1时,它会让你崩溃,错误'索引超出了数组的范围'

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