0

I´m new to iOS development and I´m losing so much time for trying something that should be easy I think. I tried to increase the height of my row, which worked, but the problem is that the image increases in size too. So I'm searching for a solution like adding a margin/padding to my row with the images still the same size, not like on the left photo? The right photo is a good example of what I'm trying to explain.

enter image description here

Does someone have a good example of code for this? Thank you! :)

Code for TableView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
    Personen *myPersoon = self.personen[indexPath.row];

    // Configure the cell...
    cell.imageView.image = [UIImage imageNamed:myPersoon.foto];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",
                           myPersoon.naam, myPersoon.voornaam];

    return cell;
}
Nedim
  • 127
  • 1
  • 2
  • 11
  • You can set margin or padding by adding top/left/right/bottom spacing constraints in your xib. If you increase the height of your tableViewCell, also take in to account that your margins also need to change (otherwise the image gets stretched). – Sander Saelmans May 08 '16 at 20:02
  • How can I exactly set constraints in my xib? I have a basic Table View and I can't select the image to add constraints. This is what I see: http://prntscr.com/b1sn1a – Nedim May 08 '16 at 20:16
  • Oh I mean setting the constraints of your cell's xib. If you don't use a custom cell yet, I would suggest making one first. In this cell add a "container" view (the view you are going to set your top/bottom/left/right spacing constraints on), and in this view you add your other views. Now in your viewController, add the tableView delegate method: heightForRowAtIndexPath, and make sure to return UITableViewAutomaticDimension. This makes sure that the height of the cell is the total height of all vertical constraints, and you don't have to do any calculations yourself. – Sander Saelmans May 08 '16 at 20:21
  • Try `cell.imageView.contentMode = UIViewContentModeCenter;`. – EricS May 08 '16 at 20:23
  • @SanderSaelmans I'm really thankful for your help, I'll try this out with another project. But now I have to make this without a custom cell :/ I was searching for a code that works fine with a basic Table View. – Nedim May 08 '16 at 20:25
  • @EricS Definitely not working. – Nedim May 08 '16 at 21:02
  • If it's not working, I would either use a custom cell or resize the images by hand so they fit. See various solutions at http://stackoverflow.com/questions/2788028/how-do-i-make-uitableviewcells-imageview-a-fixed-size-even-when-the-image-is-sm – EricS May 08 '16 at 21:17
  • @EricS I'll check this out – Nedim May 08 '16 at 22:38

1 Answers1

1

Instead of using UITableViewCell's default title and image view, create your own cell. Make a storyboard, add a cell to a tableView, and add image view/label/button or whatever, add constraints, and you can have whatever layout / positioning you'd like to have.

Your table view is quite simple and you don't want to create your own? Alternatively, you can still subclass it and use layoutSubviews: to alter the position:

- (void)layoutSubviews {

    [super layoutSubviews];

    CGRect frame = self.imageView.frame;
    frame.origin.y = (self.frame.size.height - kImageViewDiameter ) / 2.0f;
    frame.size.height = kImageViewDiameter;
    frame.size.width = kImageViewDiameter;
    self.imageView.frame = frame;
}
Stephenye
  • 806
  • 6
  • 12