所有的细胞都是一样的

时间:2013-10-30 11:13:35

标签: ios uitableview

我试图在xcode5中的storyboard中实现一个表。 在故事板上做的时候,我无法得到结果。 我试图在故事板中使用以下代码。 有人能发现这段代码有什么问题吗? 输出显示所有单元格相同。我如何用数组中的不同对象分隔每个单元格?

temp=[[HomeDetails alloc]init];
array=[[NSMutableArray alloc]init];
homecell=[[HomeCell alloc]init];

selection.Title=@"Popular";
selection.description=@"ghjggb";
selection.Image=@"PopularLogo.png";

[array addObject:selection];

selection.Title=@"Browse";
selection.description=@"gdfgdgb";
selection.Image=@"BrowseLogo.png";

[array addObject:selection];

selection.Title=@"My Signture";
selection.description=@"gdfgdfgb";
selection.Image=@"MySignatureLogo.png";

[array addObject:selection];

我已经在tableview原型中创建了tableviewcell。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell==nil)
{
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
        temp=[array objectAtIndex:indexPath.row];
        UILabel *Label = (UILabel *)[cell viewWithTag:2];
        Label.text = temp.Title;
        NSLog(@"%d",indexPath.row);
        UIImageView *Image = (UIImageView *)[cell viewWithTag:1];
        Image.image = [UIImage imageNamed:temp.Image];
        UITextField *textfield = (UITextField *)[cell viewWithTag:3];
        textfield.text =temp.description;
}

1 个答案:

答案 0 :(得分:0)

您正在将相同的对象“选择”一遍又一遍地添加到数组中。 你还在编辑同一个对象的属性 - 它将影响数组中存在的所有3个元素 - 因为它在内存中是同一个对象。

每次在设置值之前创建新实例。

selection = [Selection new]; //this is missing - each time you want to create a new one.
selection.Title=@"My Signture";
selection.description=@"gdfgdfgb";
selection.Image=@"MySignatureLogo.png";
[array addObject:selection];