Darktt
Darktt

Reputation: 184

TableViewCell image behavior issue

I have a behavior issue in UITableView,

that behavior issue on youtube.

My code here:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *fileName = (NSString *)_movies[indexPath.row];
    UIImage *thumbnail = [UIImage imageWithContentsOfFile:_movieThumbnails[indexPath.row]];

    [cell.textLabel setText:fileName];
    [cell.imageView setImage:thumbnail];
    [cell.imageView setClipsToBounds:YES];
    [cell.imageView setAutoresizingMask:UIViewAutoresizingNone];

    return cell;
}

The first thumbnail resolution is 640*360, second is 160*90,

Cell height is 60.

Test device is iPhone 5 iOS 7.1.2, and iPhone 5s iOS 7.0.6,

Those has same behavior issue.


tableView:willDisplayCell:forRowAtIndexPath: on here:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell.imageView setFrame:CGRectMake(15.0f, 0.0f, 106.0f, 60.0f)];
}

This frame size is current imageView frame by log.

Upvotes: 0

Views: 78

Answers (1)

blazejmar
blazejmar

Reputation: 1090

Ok so the reason for this behavior is that tapping delete button launches animation which changes frame of the content view and this results in this weird animation.

Solution is to create a subclass of UITableViewCell and in layoutSubviews method change the frame of the imageView and label to fit contentView properly (assuming that size of the image is always the same). This way you would end up with something like this in your TableViewDatasource:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *fileName = (NSString *)_movies[indexPath.row];
    UIImage *thumbnail = [UIImage imageWithContentsOfFile:_movieThumbnails[indexPath.row]];

    [cell.textLabel setText:fileName];
    [cell.imageView setImage:thumbnail];
}

Also if you're targeting iOS 6 and higher you can use registerClass:forCellReuseIdentifier: in UITableView and simplify the code above a bit (remove boilerplate code with allocating table view cell).

Upvotes: 1

Related Questions