NSMutableArray *tblContents = [NSMutableArray arrayWithObjects: @"1", @"2", @"3", nil];
[tblContents addObject:txtNewTableRow.text];
My app is crashing at line 2. Error message in console is 'NSInvalidArgumentException', reason: '-[NSCFString addObject:]: unrecognized selector sent to instance xxx
BTW, it works alright if i replace arraywithobjects initialization with alloc & init!
I am simply creating a mutable array and adding an object to it. Whats the problem in there?
Thanks Sridhar Reddy
Full code: .h:
#import <UIKit/UIKit.h>
@interface TableStarterViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *tblContents;
IBOutlet UITextField *txtNewTableRow;
IBOutlet UITableView *tblMain;
}
@property (nonatomic, retain) NSMutableArray *tblContents;
@property (nonatomic, retain) UITextField *txtNewTableRow;
@property (nonatomic, retain) UITableView *tblMain;
-(IBAction) addRowToTable;
@end
.m:
#import "TableStarterViewController.h"
@implementation TableStarterViewController
@synthesize tblContents;
@synthesize txtNewTableRow;
@synthesize tblMain;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tblContents count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
cell.textLabel.text = [tblContents objectAtIndex:indexPath.row];
return cell;
}
-(IBAction) addRowToTable
{
[tblContents addObject:txtNewTableRow.text];
[tblMain reloadData];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
tblContents = [NSMutableArray arrayWithObjects: @"1", @"2", @"3", nil];
[super viewDidLoad];
}
Thats all code.
addObjectmessage. In the debugger, try "po tblContents" at this line, and the XXX in your "instance XXX"-- is that the same address as tblContents?