I have been trying to achieve the below requirements to get them working. Am I doing it the right way?
We have a table with customised Cell (say:
firstViewController
)Customised Cell has a button, click event in this button, and carries row index value to another view controller. In this view controller I will use this row index to access the data model from first view controller.
Annotation: Each row has values associated with unique Dictionary value in Array based on row Index.
Pseudo Code: (pasted only necessary code)
FirstViewController.h
@interface firstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property NSMutableArray *MasterRecords; // Each element in Array is dictionary.
@property (weak, nonatomic) IBOutlet UITableView *tblMasterTable;
@end
FirstViewController.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *columnIdfer = @"items";
StudentCell *cell = (StudentCell *)[tableView dequeueReusableCellWithIdentifier:columnIdfer];
if ( cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StudentCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (indexPath.row < [self.MasterRecords count])
{
cell.iRecordIndex = indexPath.row;
}
return cell;
}
In the above code I set the iRecordIndex
to IndexPath.row
StudentCell.h
@interface StudentCell : UITableViewCell
@property NSInteger iRecordIndex;
@property (weak, nonatomic) IBOutlet UIButton *btnSubmit; // It pushes viewController.
@end
StudentCell.m
- (IBAction)btnSubmitPressed:(id)sender {
NextViewController *sVC = [[NextViewController alloc] initWithRecIndex:@"NextViewController" bundle:nil Index:self.iRecordIndex];
// Pushing NextViewController to NavigationViewController.
}
NextViewController.h
@interface NextViewController : UIViewController <MKMapViewDelegate>
- (id)initWithRecIndex:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil Index:(NSInteger )iRecIndex;
@property NSInteger iRecordIndex;
@end
NextViewController.m
- (id)initWithRecIndex:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil Index:(NSInteger )iRecIndex
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.iRecordIndex = iRecIndex;
}
return self;
}
-(void) viewWillAppear:(BOOL)animated
{
//Get FistViewController. say ==> fvc.
NSMutableArray *mRec = [fvc MasterRecords];
NSMutableDictionary *mDict = [mRec objectAtIndex:self.iRecordIndex];
NSLog (@" value1 is %@ ", [mDict valueForKey:@"value1"]);
NSLog (@" value2 is %@ ", [mDict valueForKey:@"value2"]);
// Using value1, and value2 to display MKMapView.
}
Is this the right approach, or should I improve it?