Search code examples
iosobjective-cuitableviewunrecognized-selector

How do I center a UIImage inside a UITableView cell?


I am trying to center all the UIImage images in my UITableView cell, but cannot achieve the results.

I tried searching for suggestions/solutions and found several, but nothing works.

Here is my code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

- (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];

    }

    cell.backgroundColor = [UIColor clearColor];

    cell.imageView.image = [UIImage imageNamed:[sponsorsLogoArray objectAtIndex:indexPath.row]];

    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath;
}

Added the following code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    //cell.imageView.image = [UIImage imageNamed:[sponsorsLogoArray objectAtIndex:indexPath.row]];
    UIImageView *pic = [ [UIImageView alloc] initWithImage:[array objectAtIndex:indexPath.row]];
    [cell.contentView addSubview:pic];
    pic.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);

    ....
}

And also tried:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    //cell.imageView.image = [UIImage imageNamed:[array objectAtIndex:indexPath.row]];
    UIImageView *pic = [ [UIImageView alloc] initWithImage:[array objectAtIndex:indexPath.row]];
    pic.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    [cell.contentView addSubview:pic];
    pic.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);

    ....
}

And also:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    //cell.imageView.image = [UIImage imageNamed:[array objectAtIndex:indexPath.row]];
    UIImageView *pic = [ [UIImageView alloc] initWithImage:[array objectAtIndex:indexPath.row]];
    pic.center = CGPointMake(cell.contentView.bounds.size.width / 2 , 60);
    pic.contentMode = UIViewContentModeScaleAspectFit;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    [cell.contentView addSubview:pic];

    ....
}

all 3 times received error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString _isDecompressing]: unrecognized selector sent to instance 0x10036ada0'

What am I doing wrong?


Solution

  • You need to subclass UITableViewCell and reposition the frame of the built in imageView in layoutSubviews to achieve what you want to do. You should never add subviews in cellForRowAtIndexPath: to the cell, it breaks MVC.

    static NSString * const CellIdentifier = @"CellIdentifier"
    
    @implementation
    ....
    
    // Add this in viewDidLoad
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        ....
    
        [self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:CellIdentifier];
    }
    

    Try this instead:

    // In a separate subclass of UITableViewCell
    
    - (void)layoutSubviews {
        [super layoutSubviews];
    
        self.imageView.center = CGPointMake(self.contentView.bounds.size.width/2,self.contentView.bounds.size.height/2);
    }
    
    
    // In your controller
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
        cell.backgroundColor = [UIColor clearColor];
    
        cell.imageView.image = [UIImage imageNamed:[sponsorsLogoArray objectAtIndex:indexPath.row]];
    
        return cell;
    }