[英]iOS TableView is displaying same object in every cell
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). 我有一个奇怪的问题,我正在设置一个非常基本的表视图来显示联系人,我有tabelviewcontroller,如下所示(我试图包括所有相关部分),但由于某些原因,当我运行应用程序时,我有相同的数据标签出现在每个表格单元格中(全部七个)。 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 以下所有代码都来自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];
}
I have 1 section 我有1节
- (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 所以我不确定为什么每个表格标签都说“7”,提前谢谢你
In viewDidLoad
在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. 如果没有创建新对象[[Contact alloc] init]
,则表示您正在更改同一个对象。
You will need to create a new instance of Contact
each time you add it to your data source. 每次将其添加到数据源时,都需要创建一个新的Contact
实例。 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). 您正在向数组添加相同的对象,因此基本上数组中的所有7个项目都只是指向同一对象的指针(值为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. 等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.