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