Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 tableview cell.imageview extra padding?

I have a tableview which renders perfectly in iOS 6 & has done so for years. In iO7 in the same tableview either side of the cell.imageview its adding some extra padding approx 5mm either side of each image shown below thus moving my cell.textLabel.text further to the right. How would I remove this I cant seem to find the answer anywhere to this question?

iOS image

like image 439
Alex McPherson Avatar asked Sep 30 '13 20:09

Alex McPherson


People also ask

Why is there extra padding at the top of my UITableView?

Short answer is that this extra padding is probably due to the table view header (not the section header), and that UITableView doesn't like to be assigned a header with a height of 0.0.

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


2 Answers

In iOS7, the UITableViewCell's predefined property imageView is indented towards right by 15pt by default.
And this has nothing to do with the following UITableViewCell properties

indentationLevel
indentationWidth
shouldIndentWhileEditing
separatorInset

Therefore creating your own custom UITableViewCell is the best way to overcome it.
According to Apple, there are 2 good ways to do it:

If you want the cell to have different content components and to have these laid out in different locations, or if you want different behavioral characteristics for the cell, you have two alternatives:

  • Add subviews to a cell’s content view.
  • Create a custom subclass of UITableViewCell.

Solution:

As you don't prefer subclassing UITableViewCell, so adding custom subviews is your choice.
Simply creates your own image view and text labels, and add them through code or through storyboard. e.g.

//caution: simplied example
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //get the cell object
    static NSString *CellIdentifier = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    //create your own labels and image view object, specify the frame
    UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)];
    [cell.contentView addSubview:mainLabel];

    UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0, 220.0, 25.0)];
    [cell.contentView addSubview:secondLabel];
    
    UIImageView *photo = [[UIImageView alloc] initWithFrame:CGRectMake(225.0, 0.0, 80.0, 45.0)];
    [cell.contentView addSubview:photo];
    
    //assign content
    mainLabel.text = @"myMainTitle";
    secondLabel.text = @"mySecondaryTitle";
    photo.image = [UIImage imageNamed:@"myImage.png"];
    return cell;
}

Note that as the predefined UITableViewCell content properties: cell.textLabel, cell.detailTextLabel and cell.imageView are untouched so they will remind nil and will not be shown.


Reference:

A Closer Look at Table View Cells https://developer.apple.com/Library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1

Hope this help!

like image 116
Conrad Lo Avatar answered Sep 28 '22 00:09

Conrad Lo


I probably had the same problem, the only thing that workd for me is setting the image frame:

cell.imageView.frame = CGRectMake( 0, 0, 50, 55 );

And if you are subclassing the cell, better to do:

- (void) layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = CGRectMake( 0, 0, 50, 55 );
}
like image 40
Tarek Hallak Avatar answered Sep 28 '22 02:09

Tarek Hallak