2013-12-17 58 views
0

我有一个tableview填充名称和图像的数组。我已经通过继承UITableViewCell添加了一个自定义单元格。自定义单元格包含UIImage,其模式设置为“Aspect Fill”,并将高度和宽度设置为52以整齐地放入我的自定义单元格中。当我运行应用程序时,它会加载所有图像,并按照我指定的尺寸正确地将它们放入我的容器视图中。问题是每当我滚动视图时,重用单元显示图像就好像我没有自定义单元格一样。换句话说,不正确。这里是我的cellForRowAtIndexPath方法自定义tableViewCell滚动时显示不正确

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *cellIdentifier = @"Cell"; 
    customCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    contact = contactsArray[indexPath.row]; 
    NSString *firstName = contact[@"mutableFirstName"]; 
    NSString *lastName = contact[@"mutableLastName"]; 
    UIImage *image = contact[@"mutableImage"]; 
    cell.imageView.image = image; 
    cell.firstNameLabel.text = firstName; 
    cell.lastNameLabel.text = lastName; 
    return cell; 
} 
+0

你使用故事板? – amar

+0

是的,我正在使用故事板 – user2076367

+0

然后将“单元格”设置为单元格标识符 – amar

回答

1

如果,如果你的 customCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 回报nil ... ???

将实施修改为:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"Cell"; 

    customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) { 
     cell = [[customCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.backgroundColor = [UIColor clearColor]; 
    } 

    cell.imageView.image = image; 
    cell.firstNameLabel.text = [NSString stringWithFormat:@"%@", contact[@"mutableFirstName"]]; 
    cell.lastNameLabel.text = [NSString stringWithFormat:@"%@", contact[@"mutableImage"]]; 
    return cell; 
} 

希望有所帮助。

+0

我关闭了自动布局,似乎解决了这个问题。关于Balram的建议,我的理解是,在使用故事板时,添加“if语句”以确定单元格是否存在是不必要的。那是对的吗? – user2076367

+0

除了我上面的回答,我将所有图像的大小调整为更易于管理的大小。这可能一直是我最终的问题。感谢大家。 – user2076367