Reputation: 160
I am experiencing an error at the following line:
cell1.textLabel.text = [settings objectAtIndex:indexPath.row];
And this is the uncaught inception error i receive:
Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]' First throw call stack: (0x2265012 ... 0x21a5 0x1) libc++abi.dylib: terminate called throwing an exception (lldb)
Here's my code:
.h:
@interface ViewController : UIViewController <UITableViewDataSource ,UITableViewDelegate>{
//Settings Table
IBOutlet UITableView *settingTable;
NSMutableArray *settings;
}
@property (nonatomic, retain) UITableView *settingTable;
@property (nonatomic, retain) NSMutableArray *settings;
.m:
@synthesize settingTable;
@synthesize settings;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (!settings) {
return 0;
}
if ([settings count] > 0){
return [settings count];
}
else
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (settingTable) {
NSLog(@"%@", settings);
UITableViewCell *cell1 = [settingTable dequeueReusableCellWithIdentifier:@"MainCell1"];
if (cell1 == nil) {
cell1 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell1"];
}
cell1.textLabel.text = [settings objectAtIndex:indexPath.row];
return cell1;
}
// ...
- (void)viewDidLoad
{
[super viewDidLoad];
settingTable.delegate = self;
settingTable.dataSource = self;
//settings table
settings = [[NSMutableArray alloc] initWithObjects:@"Downloads", @"Queue", @"FAQ", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
Upvotes: 0
Views: 5116
Reputation: 109
If you are using story board and static table then one of the two things can work:
1> number of cell should be same in story boad which returning number of rows.
or
2> if you want to keep one cell in story board then implement indentationLevelForRowAtIndexPath datasource methods.In rang exception if you trace the stack then you will find various data source method that you need to implement,because in dynamic prototype table it will take care it self but in static we need to implement datasource methods
Upvotes: 1
Reputation: 462
Just change it the line to:
cell1.textLabel.text = [settings objectAtIndex:indexPath.row-1];
Upvotes: -1
Reputation: 2837
The problem is that you are using static cells in UITableView. Just increase number of rows in your storyboard. It is the maximum count of cell you can add to Table View programmatically.
Sorry i didn't see that you're not using storyboard. But my answer is correct for those who are using it.
Upvotes: 4
Reputation: 20237
I would change your settings to be initialized like this:
- (void)viewDidLoad
{
[super viewDidLoad];
settingTable.delegate = self;
settingTable.dataSource = self;
}
- (NSMutableArray *)settings
{
if (!_settings) settings = [[NSMutableArray alloc] initWithObjects:@"Downloads", @"Queue", @"FAQ", nil];
return _settings;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return settings.count;
}
This way your settings object is always initialized.
Upvotes: 1
Reputation: 187
For cleaning up the code,you can do the followings :
1.In the cellForRowAtIndexPath method, use
if([settings count]>0)
instead of
if(settingTable)
2.In the numberOfRowsInSection method,use
if ([settings count] > 0){
return [settings count];
}
else
return 0;
instead of
if(!settings){
return 0;
}
if ([settings count] > 0){
return [settings count];
}
else
return 0;
The above changes in your code will make your code working perfectly.
Upvotes: -1