paul590
paul590

Reputation: 1425

NSMutableArray to UITableView

This could be a silly question but I am new to IPhone developing, anyhow I have an NSMutableArray with information loaded from a server. I am trying to put this information on a table. I searched around for some ideas on how to do this and I ran into this code which a lot of people seem to use in order to do this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [myArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath           *)indexPath
  {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

if (!cell) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease];
}

cell.textLabel.text = [myArray objectAtIndex:[indexPath row]];
NSLog(@"Cell is %@", [myArray objectAtIndex:indexPath.row]);
return cell;

}

now my question is inside the if statement it gives me two errors with the autorelease saying: is unavailable and not available on reference counting mode, and arc forbids message send of autorelease, any thoughts?? thanks for the help

Upvotes: 0

Views: 1125

Answers (4)

moosa0709
moosa0709

Reputation: 476

This is a simple tutorial link for add a NSMutableArray into the tableView...Simple Tutorial link..Hope Its useful for you.

Upvotes: 1

Matt
Matt

Reputation: 2411

This is also easily done using the freely available Sensible TableView framework. They have something called ArrayOfObjectsSection where you just pass the NSArray to and it will display it automatically, amongst many other stuff (including doning the server fetching for you).

Upvotes: 1

Jack Humphries
Jack Humphries

Reputation: 13267

You are using Automatic Reference Counting, which takes care of memory management for you (for the most part). Remove all references to manual memory management, such as autorelease (and retain, release, etc.), and the app will build. Use this here below:

if (!cell) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];

 }

Upvotes: 3

rmaddy
rmaddy

Reputation: 318774

The code you have is older code and only works as-is with manual reference counting (MRC). Newer projects use automatic reference counting (ARC) by default. Just remove the call to autorelease and you will be fine.

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];

Upvotes: 3

Related Questions