2014-05-23 52 views
3

我有一个奇怪的问题,我设置了一个非常基本的表视图来显示联系人,我有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” 预先感谢您

+3

你需要'alloc init''联系'对象来获取新对象。 – Akhilrajtr

+3

+1对于您的第一篇文章是清晰的并包含相关信息。这些日子非常罕见。 – rmaddy

+0

@NamoNamo请不要发表评论,要求OP看看你的答案。答案本身就够了。 – rmaddy

回答

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 
} 

如果没有创建新的对象[[Contact alloc] init] ,你正在改变同一个对象。

+0

哇谢谢你的回应,我没有意识到添加一个对象到数组没有创建它的副本 – shane

+0

@ user3667450欢迎你。快乐的编码。 – Akhilrajtr

1

它应该是这样的

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]; 
+0

不需要为每个变量分开变量。只需重复使用“联系人”。 – rmaddy

+0

它会更好地理解为新的目标c –

+0

重用代码....如果你想告诉某人谁是新的目标c。 – Jitendra

1

这是因为你在每个索引添加相同的对象。每次将它添加到数组之前,您都需要初始化一个新对象。见下面的例子

Contact *contact1 = [[Contact alloc] init]; 
contact.name = @"1"; 
[self.contacts addObject:contact1]; 
Contact *contact2 = [[Contact alloc] init]; 
contact.name = @"2"; 
[self.contacts addObject:contact2]; 
2

您需要将每次它添加到您的数据源一次创建的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]; 
    } 

希望这会有所帮助。

1

您需要像下面的代码一样每次创建新的对象。

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]; 
1

你加入同一个对象的数组,所以基本上所有的数组中的7个项目仅会指向同一个对象(与值7)。尝试为每个号码创建一个新的联系人,所以像

Contact *contact1 = [[Contact alloc] init]; 
Contact *contact2 = [[Contact alloc] init]; 
Contact *contact3 = [[Contact alloc] init]; 

等。

相关问题