How do I make UITableViewCell's ImageView a fixed size even when the image is smaller

后端 未结 16 2424
不思量自难忘°
不思量自难忘° 2020-11-27 10:27

I have a bunch of images I am using for cell\'s image views, they are all no bigger than 50x50. e.g. 40x50, 50x32, 20x37 .....

When I load the table view, the tex

相关标签:
16条回答
  • 2020-11-27 10:52

    It's not necessary to rewrite everything. I recommend doing this instead:

    Post this inside your .m file of your custom cell.

    - (void)layoutSubviews {
        [super layoutSubviews];
        self.imageView.frame = CGRectMake(0,0,32,32);
    }
    

    This should do the trick nicely. :]

    0 讨论(0)
  • 2020-11-27 10:53

    Here is @germanattanasio 's working method, written for Swift 3

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...
        cell.imageView?.image = myImage
        let itemSize = CGSize(width:42.0, height:42.0)
        UIGraphicsBeginImageContextWithOptions(itemSize, false, 0.0)
        let imageRect = CGRect(x:0.0, y:0.0, width:itemSize.width, height:itemSize.height)
        cell.imageView?.image!.draw(in:imageRect)
        cell.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
    }
    
    0 讨论(0)
  • 2020-11-27 10:54

    If you use cell.imageView?.translatesAutoresizingMaskIntoConstraints = false you can set constraints on the imageView. Here's a working example I used in a project. I avoided subclassing and didn't need to create storyboard with prototype cells but did take me quite a while to get running, so probably best to only use if there isn't a simpler or more concise way available to you.

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    
    
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: String(describing: ChangesRequiringApprovalTableViewController.self))
    
        let record = records[indexPath.row]
    
        cell.textLabel?.text = "Title text"
    
        if let thumb = record["thumbnail"] as? CKAsset, let image = UIImage(contentsOfFile: thumb.fileURL.path) {
            cell.imageView?.contentMode = .scaleAspectFill
            cell.imageView?.image = image
            cell.imageView?.translatesAutoresizingMaskIntoConstraints = false
            cell.imageView?.leadingAnchor.constraint(equalTo: cell.contentView.leadingAnchor).isActive = true
            cell.imageView?.widthAnchor.constraint(equalToConstant: 80).rowHeight).isActive = true
            cell.imageView?.heightAnchor.constraint(equalToConstant: 80).isActive = true
            if let textLabel = cell.textLabel {
                let margins = cell.contentView.layoutMarginsGuide
                textLabel.translatesAutoresizingMaskIntoConstraints = false
                cell.imageView?.trailingAnchor.constraint(equalTo: textLabel.leadingAnchor, constant: -8).isActive = true
                textLabel.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
                textLabel.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
                let bottomConstraint = textLabel.bottomAnchor.constraint(equalTo: margins.bottomAnchor)
                bottomConstraint.priority = UILayoutPriorityDefaultHigh
                bottomConstraint.isActive = true
                if let description = cell.detailTextLabel {
                    description.translatesAutoresizingMaskIntoConstraints = false
                    description.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
                    description.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
                    cell.imageView?.trailingAnchor.constraint(equalTo: description.leadingAnchor, constant: -8).isActive = true
                    textLabel.bottomAnchor.constraint(equalTo: description.topAnchor).isActive = true
                }
            }
            cell.imageView?.clipsToBounds = true
        }
    
        cell.detailTextLabel?.text = "Detail Text"
    
        return cell
    }
    
    0 讨论(0)
  • 2020-11-27 10:55

    The whole cell doesn't need to be remade. You could use the indentationLevel and indentationWidth property of tableViewCells to shift the content of your cell. Then you add your custom imageView to the left side of the cell.

    0 讨论(0)
  • 2020-11-27 10:56

    image view add as a sub view to the tableview cell

    UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 90, 70)];
    imgView.backgroundColor=[UIColor clearColor];
    [imgView.layer setCornerRadius:8.0f];
    [imgView.layer setMasksToBounds:YES];
    [imgView setImage:[UIImage imageWithData: imageData]];
    [cell.contentView addSubview:imgView];
    
    0 讨论(0)
  • 2020-11-27 11:00

    Better create an image view and add it as a sub view to the cell.Then you can get the desired frame size.

    0 讨论(0)
提交回复
热议问题