Re: Table View with NSMutableArray of objects
Re: Table View with NSMutableArray of objects
- Subject: Re: Table View with NSMutableArray of objects
- From: Robert Martin <email@hidden>
- Date: Thu, 18 Oct 2012 07:49:59 -0400
On Oct 18, 2012, at 6:39 AM, luis javier markina <email@hidden> wrote:
>
> Hi, I am developing an app and i face this problem.
> I have a Table view with its view controller and i am trying to fill this table with some data from a NSMutableArray.
...
> - (void)viewDidLoad
> {
> Lugar *lugar = [[Lugar alloc] init];
…
> for (NSUInteger i = 0; i < 6; i++) {
> lugar.nombre = [nombres objectAtIndex:i];
> lugar.direccion = [direcciones objectAtIndex:i];
> lugar.descripcion = [descripciones objectAtIndex:i];
> lugar.precio = [precios objectAtIndex:i];
> [self.datosTabla insertObject:lugar atIndex:i];
> }
> ...
> The app runs properly but it shows me just the name of the last
> object add to the mutableArray, in this case the restaurant called: Plan
> B. So i have just get a table with 6 rows with the same restaurant
> information.
Luis, You are setting all the elements for your table data array to the same object.
Your loop just updates this object, so all the elements end up pointing to the same instance, which contains the last values you set.
Try this instead:
Lugar *lugar = nil; // don't allocate the object outside the loop
for (NSUInteger i = 0; i < 6; i++) {
lugar = [[Lugar alloc] init];
lugar.nombre = [nombres objectAtIndex:i];
lugar.direccion = [direcciones objectAtIndex:i];
lugar.descripcion = [descripciones objectAtIndex:i];
lugar.precio = [precios objectAtIndex:i];
[self.datosTabla insertObject:lugar atIndex:i];
[lugar release]; // unless you're using ARC
}
Rob
PS: This isn't really an Xcode question - you should probably have sent it to the Cocoa-dev list
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden