2017-04-23 4 views
2

Ich habe ein Problem mit UITableViewAutomaticDimension:UITableViewAutomaticDimension mit programmatisch hinzugefügt Bildern

In meinem Tableview Ich habe mehrere imageviews programmatisch mit berechneter Höhe erstellt:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "managePlayset", for: indexPath) as! ManagePlaysetTableViewCell 
    let playset = playsets[indexPath.row] 
    cell.playsetName.text = playset.name 
    if (playset.musicItems?.count)! > 0 { 
     var i = 0 

     for musicItem in (playset.musicItems?.array)! { 

      let imageView = UIImageView(image: (musicItem as! MusicItem).getFirstPhoto()) 
      imageView.frame = CGRect(x: cell.contentView.frame.origin.x+CGFloat(i)*(imageWidth+10.0), y: cell.contentView.frame.origin.y+40, width: imageWidth, height: imageWidth) 
      imageView.contentMode = .scaleAspectFit 
      cell.contentView.addSubview(imageView) 
      let constraint = NSLayoutConstraint(item: imageView, 
      attribute: NSLayoutAttribute.bottomMargin, relatedBy: 
      NSLayoutRelation.equal, toItem: cell.contentView, 
      attribute: NSLayoutAttribute.bottomMargin, multiplier: 1, 
      constant: 0) 
      cell.contentView.addConstraint(constraint) 

      i = i + 1 
     } 
    } 

    cell.editButton.tag = indexPath.row 
    return cell 
} 

In viewDidLoad I gesetzt:

let width = view.frame.width 
    imageWidth = width/CGFloat(maxItemsOnPage) - 10.0 
    tableView.estimatedRowHeight = imageWidth + 20.0 
    tableView.rowHeight = UITableViewAutomaticDimension 

Aber Höhe ist nicht richtig eingestellt (Bilder sind abgeschnitten) Jeder Rat wird sehr geschätzt!

+0

Sie müssen Einschränkungen hinzufügen (programmatisch, da Sie die 'UIImageView's programmatisch hinzufügen), der die Höhe des' UITableViewCell' definieren, basierend auf der Höhe eines 'UIImageView' (oder was auch immer definiert die Höhe der Eine 'UITableViewCell', weil' UITableViewAutomaticDimension' mit Constraints arbeitet. –

+0

Ich habe Einschränkungen programmgesteuert hinzugefügt, aber es hilft aus irgendeinem Grund nicht –

Antwort

1

Ich denke, es ist, weil UIImageView programmgesteuert erstellt. Dann verwendet es kein automatisches Layout-System. Um es zu ändern, müssen Sie imageView.translatesAutoresizingMaskIntoConstraints = false in Ihrer Schleife hinzufügen.

0

Haben Sie versucht, die obere und untere Kante von imageView zu beschränken? dh

let constraintTop = NSLayoutConstraint(item: imageView, 
     attribute: NSLayoutAttribute.top, relatedBy: 
     NSLayoutRelation.equal, toItem: cell.contentView, 
     attribute: NSLayoutAttribute.topMargin, multiplier: 1, 
     constant: 0) 
let constraintBottom = NSLayoutConstraint(item: imageView, 
     attribute: NSLayoutAttribute.bottom, relatedBy: 
     NSLayoutRelation.equal, toItem: cell.contentView, 
     attribute: NSLayoutAttribute.bottomMargin, multiplier: 1, 
     constant: 0) 
cell.contentView.addConstraints([constraintTop, constraintBottom]) 
Verwandte Themen