我有一个奇怪的问题,我设置了一个非常基本的表视图来显示联系人,我有tabelviewcontroller如下所示(我试图包括所有相关部分),但对于某些原因,当我运行应用程序我有相同的数据标签出现在每个表格单元格(全部七)。可能有人请查看错误我不能......非常感谢iOS TableView在每个单元格中显示相同的对象
所有下面的代码是从tableviewcontroller.m 我让我在视图阵列做负载
- (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];
}
我有1节
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
我在阵列的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.contacts count];
}
I C reate这里我细胞
- (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;
}
所以我不知道为什么每一个表格单元格标签上写着“7” 预先感谢您
你需要'alloc init''联系'对象来获取新对象。 – Akhilrajtr
+1对于您的第一篇文章是清晰的并包含相关信息。这些日子非常罕见。 – rmaddy
@NamoNamo请不要发表评论,要求OP看看你的答案。答案本身就够了。 – rmaddy