-3

i'm using the fellowing code and didSelectRowAtIndexPath is returning the wrong value and crashing by random selection. Pl advice.

View Controller.h

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

View 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
rmaddy
  • 314,917
  • 42
  • 532
  • 579
satishiOS25
  • 531
  • 4
  • 11
  • 3
    @JoshCaswell It's amazing how many questions have been posted due to this little typo. Any time someone mentions an indexPath issue with "didSelectRow..." I now first check for the mistaken "didDeselectRow...". – rmaddy Jul 16 '14 at 06:41
  • 1
    Please stop using the Xcode tag. It has nothing at all to do with your question. – rmaddy Jul 16 '14 at 06:42
  • I'm looking for a solution than crap comments and suggestion thanks it's not your port to insist me, you can move on if you are not interested in this question rather than poking. – satishiOS25 Jul 16 '14 at 06:46
  • 3
    @user3807171 I gave you the solution by marking your question as a duplicate. You are using the wrong method. See the accepted answer in the duplicate question. This is how this site works. – rmaddy Jul 16 '14 at 06:51

1 Answers1

1

Its because of IndexPath.Row + 1. Why you are using it by the way? any good reason ? It will lead to crash buddy. And this is what happening

Array indexing starts from 0 and so is of indexPath.row. When you will try to call indexPath.row+1 it will give you crash with error 'index was out of bound of array'

In didSelectRowAtIndexPath change every indexPath.row+1 to indexPath.row. Your problem is solved.

Shahab Qureshi
  • 952
  • 1
  • 7
  • 19