3

I have a weird problem, I am setting up a very basic table view to display contacts and I have the tabelviewcontroller as shown below (I tried to include all relevant portions), but for some reason when I run the app I have the same data label appear in each table cell (all seven). Could someone please see the error I can't... thanks a lot

All the following code is from tableviewcontroller.m I make my array in view did load

- (void)viewDidLoad
{
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;

self.contacts = [NSMutableArray arrayWithCapacity:10];
Contact *contact = [[Contact alloc] init];
contact.name = @"1";
[self.contacts addObject:contact];
contact.name = @"2";
[self.contacts addObject:contact];
contact.name = @"3";
[self.contacts addObject:contact];
contact.name = @"4";
[self.contacts addObject:contact];
contact.name = @"5";
[self.contacts addObject:contact];
contact.name = @"6";
[self.contacts addObject:contact];
contact.name = @"7";
[self.contacts addObject:contact];
}

I have 1 section

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

I have the number of rows in my array

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.contacts count];
}

I create my cells here

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContactCell"];
Contact *contact = [self.contacts objectAtIndex:[indexPath row]];
cell.contactNameLabel.text = contact.name;
return cell;
}

So I'm not sure why every table cell label says "7" thank you in advance

3
  • 3
    You need to alloc init Contact object for new object. Commented May 23, 2014 at 4:11
  • 3
    +1 for your first post being clear and containing relevant info. So rare these days. Commented May 23, 2014 at 4:14
  • @NamoNamo Please don't post a comment asking the OP to look at your answer. The answer itself is enough. Commented May 23, 2014 at 4:19

6 Answers 6

5

In viewDidLoad

- (void)viewDidLoad {

     self.contacts = [NSMutableArray arrayWithCapacity:10];
     Contact *contact = [[Contact alloc] init];
     contact.name = @"1";
     [self.contacts addObject:contact];

     //Create new instance of Contact
     contact = [[Contact alloc] init];
     contact.name = @"2";
     [self.contacts addObject:contact];

     //Add objects same in way
}

If not created new object [[Contact alloc] init], you were changing the same object.

Sign up to request clarification or add additional context in comments.

1 Comment

wow thanks for the response, I didn't realize adding an object to the array didn't create a copy of it
2

You will need to create a new instance of Contact each time you add it to your data source. Like so

self.contacts = [NSMutableArray arrayWithCapacity:10];
    for(int i=1; i<8;i++) {
        Contact *contact = [[Contact alloc] init];
        contact.name = <some value>;
        [self.contacts addObject:contact];
    }

Hope this helps.

Comments

1

This is because you are adding same object in each index. You need initialize a new object each time before you adding it to the array. See the below example

Contact *contact1 = [[Contact alloc] init];
contact.name = @"1";
[self.contacts addObject:contact1];
Contact *contact2 = [[Contact alloc] init];
contact.name = @"2";
[self.contacts addObject:contact2];

Comments

1

You need to create new object everytime like below code.

self.contacts = [NSMutableArray arrayWithCapacity:10];
Contact *contact = [[Contact alloc] init];
contact.name = @"1";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];
contact.name = @"2";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];

contact.name = @"3";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];

contact.name = @"4";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];

contact.name = @"5";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];

contact.name = @"6";
[self.contacts addObject:contact];

contact = [[Contact alloc] init];

contact.name = @"7";
[self.contacts addObject:contact];

Comments

1

You are adding the same object to the array, so basically all the 7 items in your array are just pointers to the same object (with the value 7). Try create a new contact for each number, so like

Contact *contact1 = [[Contact alloc] init];
Contact *contact2 = [[Contact alloc] init];
Contact *contact3 = [[Contact alloc] init];

ect.

Comments

1

It should be like this

First Contact

 Contact *contact = [[Contact alloc] init];
    contact.name = @"1";
    [self.contacts addObject:contact];

Second Contact

    contact = [[Contact alloc] init];
contact.name = @"2";
[self.contacts addObject:contact];

3 Comments

No need for separate variables for each. Just reuse contact.
it will understood better for the one who is new to objective c
@NamoNamo there are many ways to answer above question. but i prefer the way that he would understood better with this. if u think there is better answer with this then you should give your answer. lots of people says that instead of this you should do this. i appreciate for that but there is no wrong with above answer. if i reuse the code then may be he just copy and paste the answer

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.