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
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.
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.
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];
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];
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];
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.