In every UITableViewCell of a UITableView I\'m placing an image in its imageView. However, if I increase the height of a cell using tableView:heightForRowAtIndexPath: to acc
The moment you think you need to adjust sizes or positions of items in the default styles, is the time you need to think about creating a custom cell with your own items sized and positioned the way you want them to be. This is why you can create custom cells. :)
A really useful function you might not have tried (in UITableViewDelegate
) is tableView:willDisplayCell:forRowAtIndexPath:
. It is called immediately before a cell is displayed and its purpose is to enable you to do things to the layout without the OS modifying it again before display. Effectively you have total control. I've used it for changing fonts in cells but you can iterate the cell's subviews and modify their frames too.
Still I agree you are at the point where it is time to look into custom cells and stop fighting the OS. It isn't hard to do and Apple gives a good example in Table View Programming Guide for iOS.
Swift 4 variation of TomSwift's solution (which worked for me nicely):
override public func layoutSubviews() {
super.layoutSubviews()
imageView?.frame = CGRect( x: 16, y: 9, width: imageView!.intrinsicContentSize.width, height: imageView!.intrinsicContentSize.height)
}
You can create a custom cell and place the objects where you need them to be as well as setting the auto mask values.
You need to subclass UITableViewCell and override layoutSubviews, as follows:
- (void) layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake( 10, 10, 50, 50 ); // your positioning here
}
In your cellForRowAtIndexPath: method, be sure to return an instance of your new cell type.
Another way (works in iOS 6) is to create a UIImageView and add it to the cell as a subview
UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,20,20)];
[yourImageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row]]]; //You can have an Array with the name of
[yourcell.contentView addSubview:yourImageView]; //add the imageView as a subview