2017-03-10 13 views
0

xcode/swiftの新機能です。私はiPhone 4インチの画面でUITableViewCellのUIImageViewを置き換える問題があります。私は3で正しい画像を埋め込んでいます:Assets.xcassetsで640,750,1242ピクセルの幅。uitableviewcell uiimage 4インチiphoneの自動レイアウト問題

TableViewとContentViewの制約[xCodeから後続スペースへ、先頭スペースから下スペース、上スペースから]は制限されます。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let identifier = "reusedCell" 
     var cell = tableView.dequeueReusableCell(withIdentifier: identifier) 

     if(cell == nil) { 
      cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: identifier) 
     } 

     let image = UIImage(named: "TableView\(indexPath.row)") 
     let imageView = UIImageView(image: image) 
     cell?.addSubview(imageView) 
     imageView.contentMode = UIViewContentMode.scaleAspectFill 
     imageView.clipsToBounds = true 

     return cell! 
} 

Click here to see images are cut from right side

画像が正しく4.7にサイズ設定されており、5.5インチのiPhoneの画面ではなく、iphone 5、5SとSEで:

これは私がテーブルビューのcellForRowAtにUIImageViewを使用する方法です。

ご迷惑をおかけして申し訳ありません。あなたがimageViewの制約を加える必要があり おかげ

答えて

0

let imageView = UIImageView(image: image) 
cell?.addSubview(imageView) 
imageView.translatesAutoresizingMaskIntoConstraints = false 
// for example 
imageView.anchor(topAnchor, left: cell.leftAnchor, bottom: cell.bottomAnchor, right: cell.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0) 

またはカスタムセル作成:

class CustomTableViewCell: UITableViewCell { 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: .subtitle, reuseIdentifier: reuseIdentifier) 

     setupViews() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    let let imageView: UIImageView = { 
     let imgView = UIImageView() 
     imgView.translatesAutoresizingMaskIntoConstraints = false 
     imgView.contentMode = UIViewContentMode.scaleAspectFill 
     imgView.clipsToBounds = true 


     return imgView 
    }() 

    func setupViews() { 
     addSubview(imageView) 
     imageView.anchor(topAnchor, left: cell.leftAnchor, bottom: cell.bottomAnchor, right: cell.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0) 
    } 
} 

は、その後にあなたの関数のtableViewを置き換えます

let identifier = "reusedCell" 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: identifier) as ! CustomTableViewCell 

     let image = UIImage(named: "TableView\(indexPath.row)") 
     cell.imageView.image = image 

     return cell! 
} 
関連する問題