Positioning the imageView of a UITableViewCell

前端 未结 6 1275
走了就别回头了
走了就别回头了 2020-12-16 13:53

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

相关标签:
6条回答
  • 2020-12-16 14:23

    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. :)

    0 讨论(0)
  • 2020-12-16 14:24

    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.

    0 讨论(0)
  • 2020-12-16 14:27

    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)
        }
    
    0 讨论(0)
  • 2020-12-16 14:33

    You can create a custom cell and place the objects where you need them to be as well as setting the auto mask values.

    0 讨论(0)
  • 2020-12-16 14:43

    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.

    0 讨论(0)
  • 2020-12-16 14:46

    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
    
    0 讨论(0)
提交回复
热议问题