Reputation: 43
I am new to Xcode and Objective-C programming and need some help.
I am looking to create a basic program for the iOS that uses hierarchal-data and 2 separate UITableViews
. I want the second UITableView
to be populated by an array that is passed between viewControllers
, based on which cell/row is selected in the first UITableView
.
The program compiles but I get a SIGABRT
error when running the program. Can someone help me fix the SIGABRT
and pass the mainArray
to the second tableView
?
Here is how far I have gotten.
My code:
ArrayTableViewController.h
@interface arrayTableViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *mainArray;
@property (nonatomic, strong) NSMutableArray *secondArray;
@property (nonatomic, strong) NSMutableArray *thirdArray;
@end
ArrayTableViewController.m
#import "ArrayTableViewController.h"
#import "table2.h"
@implementation arrayTableViewController
@synthesize mainArray, secondArray, thirdArray;
-(void) viewDidLoad {
[super viewDidLoad];
mainArray = [[NSMutableArray alloc] initWithObjects: secondArray, thirdArray, nil];
secondArray = [[NSMutableArray alloc] initWithObjects: @"123", @"456", nil];
thirdArray = [[NSMutableArray alloc] initWithObjects: @"78", @"90", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mainArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [mainArray objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
table2 *table2Controller = [[table2 alloc] initWithNibName:@"table2" bundle:nil];
table2Controller.arrayForDisplay = [[mainArray objectAtIndex: [indexPath row]] objectAtIndex:1];
[self.navigationController pushViewController:table2Controller animated:YES];
}
@end
table2.h
#import <UIKit/UIKit.h>
@interface table2 : UITableViewController
@property (nonatomic, strong) NSArray *arrayForDisplay;
@end
table2.m
@implementation table3
@synthesize arrayForDisplay;
Then the same cell configuration style that was used in ArrayTableViewController.m
Edits:
After making the necessary changes, when I run the program and select a row, I get a SIGABRT
error at the following line.
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([ArrayTableAppDelegate class]));
}
}
What would you recommend? Should I turn off ARC
and call my own releases? so that I can get to the second tableView
?
Upvotes: 1
Views: 6735
Reputation: 17478
First mistake:
In viewDidLoad
method, you have created the mainArray
with secondArray
and thirdArray
as elements even before you allocated those arrays.
Second mistake:
In cellForRowAtIndexPath:
method:
Check the line cell.textLabel.text = [mainArray objectAtIndex:[indexPath row]];
Actually the textLabel
expects a NSString value to set. But you are setting an array.
Edit:
Set the value to textLabel as following:
cell.textLabel.text = [[mainArray objectAtIndex:[indexPath row]] objectAtIndex:0];
Actually this will set the first value of the array. But that it depends on your requirement.
Edit 2:
arrayForDisplay
is an array variable but you are setting a string variable to that in the statement
table2Controller.arrayForDisplay = [[mainArray objectAtIndex: [indexPath row]] objectAtIndex:1];
Do it as follows:
table2Controller.arrayForDisplay = [mainArray objectAtIndex: [indexPath row]];
Upvotes: 3