我有一个奇怪的问题,我正在设置一个非常基本的表格视图来显示联系人,并且我具有如下所示的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];
}
我有一节
- (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]
,则您正在更改同一对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句