this is I think basic problem because stackoverflow is full of it and ofc google too. But nothing helped me. I need to pass integer vaule which says which row is selected, to another class. Here is my code:
TableViewController.h
#import <UIKit/UIKit.h>
@interface TableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
NSArray *tabledata;
}
@property (nonatomic, retain) NSArray *tabledata;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic) NSInteger selectedRow;
@end
TableViewController.m
#import "Instruments.h"
@interface Instruments ()
@end
@implementation Instruments
@synthesize tabledata;
@synthesize tableView;
@synthesize selectedRow;
- (void)viewDidLoad
{
[super viewDidLoad];
tabledata = [[NSArray alloc] initWithObjects:@"Row1", @"Row2", @"Row3", nil];
self.selectedRow = 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tabledata count];
}
- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableview dequeueReusableCellWithIdentifier:@"Row1"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Row1"];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
if (indexPath.row == self.selectedRow)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row != self.selectedRow) {
self.selectedRow = indexPath.row;
}
[tableView reloadData];
}
- (void)viewDidUnload {
[self setTableView:nil];
[super viewDidUnload];
}
@end
FirstViewController.h
//Some code which calls my variable 'selectedrow'
...
FirstViewController.m
...
if selectedrow = 0 {
//some code
}
if selectedrow = 1 {
//some code
}
if selectedrow = 2 {
//some code
}
...
How should I declare my variable ‘selectedrow’ to be able to use it in FirstViewController?
In the MVC world, everything that you intend to share among multiple view controllers belongs in the model. Your model class is globally available; one controller sets the value; the other reads it.
Header: MyAppModel.h
Implementation: MyAppModel.m
Usage: your view controllers