iphone – How to update numberOfRowsInSection using NSMutableArray count – Education Career Blog

In my app I allow users to add new rows of NSStrings to a UITableView. I save these new strings in an NSMutableArray *dataArray

So for numberOfRowsInSection I return dataArray count, but it seems to just return 0 all the time so my table is not auto-populating.

If it helps, when I do an NSLog of dataArray count as a add more entries, it keeps remaining as 0. Why isn’t it increasing?

Any help?

,

Without looking @ your code ,it’s not possible to predict the reason..

Then also you can try your HardLuck…below:

First try to NSLog your dataArray to check wether the record is adding in that or not.

It’s its ok… you are getting the records.

then you are missing one thing.

reload the table..
tableView reloadData;

I think this would definitely help you..otherwise put some code over here.

,

Have you initialised your Array. Here is how I’ve done it – prob a better way!

.h:

@interface RootViewController : UITableViewController {
NSArray *array;
}

@property (nonatomic, retain) NSArray *array;

@end

.m:

@implementation RootViewController
@synthesize array;

- (void)viewDidLoad {
    super viewDidLoad;

    NSMutableArray *mArry = NSMutableArray alloc init;
    mArry addObject:@"An ObecjT";
    self.array = mArry;
    mArry release;

}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return array count;
}


// Customize the appearance of table view cells.
- (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 autorelease;
    }

    // Configure the cell.
    cell.textLabel.text = array objectAtIndex:indexPath.row;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *aNewArray = NSMutableArray alloc initWithArray:array;
    aNewArray addObject:@"Your Object";
    self.array = aNewArray;
    aNewArray release;
    self.tableView reloadData;

}
- (void)viewDidUnload {
    self.array = nil;
}


- (void)dealloc {
    array release;
    super dealloc;
}

,

Did you set the delegate for the UITableView in Interface Builder ? please check it.

If you have added, then you have to check with that dataArray whether it is having records or not.

Leave a Comment