1

I have a Table view with its view controller and i am trying to fill this table with some data from a NSMutableArray.

I have a class called Lugar.m and Lugar.h, with some properties (with the @synthesize in the .m file) as i show:

@property (nonatomic, strong) NSString *nombre;
@property (nonatomic, strong) NSString *direccion;
@property (nonatomic, strong) NSString *descripcion;
@property (nonatomic, strong) NSString *precio;

Then I have the controller of the table view with the following viewDidLoad method

- (void)viewDidLoad
{
[super viewDidLoad];
self.title =@"Comer";
Lugar *lugar = [[Lugar alloc] init];
self.datosTabla = [[NSMutableArray alloc] initWithCapacity:6];
NSArray *nombres = [[NSArray alloc] initWithObjects:@"Masala",@"Italiano",@"Gaia",@"Pekaditos",@"Ojeda",@"Plan B", nil];
NSArray *direcciones = [[NSArray alloc] initWithObjects:@"C/San Roque, 5", @"C/ Moneda, 7 ",@"C/Nueva, 6", @"C/San Roque, 5", @"C/ Moneda, 7 ",@"C/Nueva, 6", nil];
NSArray *descripciones = [[NSArray alloc] initWithObjects:@"Bonito restaurante ecologico",@"Restaurante fino y caro", @"El mejor marisco de burgos",@"Calida recio mud", @"Calidad insuperable" , @"Calidad insuperable",nil];
NSArray *precios = [[NSArray alloc] initWithObjects:@"25€", @"15€", @"12€", @"6€", @"34€", @"34€",nil];
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];
}

In this file i have also the cellForRowAtIndex

- (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];
    Lugar *tempName =[self.datosTabla objectAtIndex:indexPath.row];
    cell.textLabel.text = tempName.nombre;
    }
    return cell;
    }

I have also the methods that return the rows and the sections

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.

I tried to fix it and i spend a lot of hours with no result. I would thank any help. Thanks in advance Luis

PD: the information of the arrays is in Spanish, sorry about that

1 Answer 1

1

You are inserting the same object into the datosTabla array 5 times. Even though you are changing the properties of the lugar object on each iteration of the for-loop, the pointer that points to the lugar object is not changing.

Make sure you allocate the lugar object inside the for-loop so there are 5 distinct objects.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title =@"Comer";
    self.datosTabla = [[NSMutableArray alloc] initWithCapacity:6];
    NSArray *nombres = [[NSArray alloc] initWithObjects:@"Masala",@"Italiano",@"Gaia",@"Pekaditos",@"Ojeda",@"Plan B", nil];
    NSArray *direcciones = [[NSArray alloc] initWithObjects:@"C/San Roque, 5", @"C/ Moneda, 7 ",@"C/Nueva, 6", @"C/San Roque, 5", @"C/ Moneda, 7 ",@"C/Nueva, 6", nil];
    NSArray *descripciones = [[NSArray alloc] initWithObjects:@"Bonito restaurante ecologico",@"Restaurante fino y caro", @"El mejor marisco de burgos",@"Calida recio mud", @"Calidad insuperable" , @"Calidad insuperable",nil];
    NSArray *precios = [[NSArray alloc] initWithObjects:@"25€", @"15€", @"12€", @"6€", @"34€", @"34€",nil];
    for (NSUInteger i = 0; i < 6; i++) {
        Lugar *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];
    }
}

You need to populate the cell's text field every time that method is called, not just when cell == nil.

- (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];
    }

    Lugar *tempName =[self.datosTabla objectAtIndex:indexPath.row];
    cell.textLabel.text = tempName.nombre;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for the answer but it still doesnt work fine. It just show me the last restaurant. Any ideas?
I overlooked the source of the issue, I've updated my answer with both issues and fixes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.