iOS TableView在每个单元格中显示相同的对象

sha*_*ane 3 objective-c uitableview nsmutablearray ios

我有一个奇怪的问题,我正在设置一个非常基本的表视图来显示联系人,我有tabelviewcontroller,如下所示(我试图包括所有相关部分),但由于某些原因,当我运行应用程序时,我有相同的数据标签出现在每个表格单元格中(全部七个).有人可以看到错误我不能...非常感谢

以下所有代码都来自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];
}
Run Code Online (Sandbox Code Playgroud)

我有1节

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

我有我的数组中的行数

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

我在这里创建我的细胞

- (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;
}
Run Code Online (Sandbox Code Playgroud)

所以我不确定为什么每个表格标签都说"7",提前谢谢你

Akh*_*jtr 5

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
}
Run Code Online (Sandbox Code Playgroud)

如果未创建新对象[[Contact alloc] init],则表示您正在更改同一对象.