Reputation: 65
I have a tableview populated with an array of names and images. I’ve added a custom cell by subclassing UITableViewCell. The custom cell contains a UIImage that has the mode set to “Aspect Fill” and the height and width set to 52 to fit neatly into my custom cell. When I run the application it loads all the images and correctly fits them into my container view at the dimensions I’ve specified. The problem is whenever I scroll the view, the reused cells displays the images as if I did not have a custom cell. In other words, incorrectly. Here is my cellForRowAtIndexPath method
-(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;
}
Upvotes: 0
Views: 63
Reputation: 5667
What if, if your
customCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
returns nil
...???
Modify your implementation as :
-(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;
}
Hope that helps.
Upvotes: 1