[英]iOS TableView is displaying same object in every cell
我有一个奇怪的问题,我正在设置一个非常基本的表视图来显示联系人,我有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];
}
我有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];
}
我在这里创建我的细胞
- (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”,提前谢谢你
在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
}
如果没有创建新对象[[Contact alloc] init]
,则表示您正在更改同一个对象。
每次将其添加到数据源时,都需要创建一个新的Contact
实例。 像这样
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];
}
希望这可以帮助。
它应该是这样的
First Contact
Contact *contact = [[Contact alloc] init];
contact.name = @"1";
[self.contacts addObject:contact];
第二次接触
contact = [[Contact alloc] init];
contact.name = @"2";
[self.contacts addObject:contact];
这是因为您在每个索引中添加相同的对象。 每次将新对象添加到数组之前,都需要初始化它。 请参阅以下示例
Contact *contact1 = [[Contact alloc] init];
contact.name = @"1";
[self.contacts addObject:contact1];
Contact *contact2 = [[Contact alloc] init];
contact.name = @"2";
[self.contacts addObject:contact2];
您需要每次都像下面的代码一样创建新对象。
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];
您正在向数组添加相同的对象,因此基本上数组中的所有7个项目都只是指向同一对象的指针(值为7)。 尝试为每个号码创建一个新联系人,就像这样
Contact *contact1 = [[Contact alloc] init];
Contact *contact2 = [[Contact alloc] init];
Contact *contact3 = [[Contact alloc] init];
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.