この問題の説明の後にコードが続きます。0〜2枚の画像を持つUITableViewCellの高さを変更するための制約を設定するにはどうすればよいですか?
私はUITableView
を含むUIViewController
を持っていますが、UITableViewCell
の画像は1枚、2枚、または0枚の画像のいずれかを示しています。 1枚と2枚の画像では、セルの高さは常に72ですが、画像がなければゼロにする必要があります。私はUIView
を2つ持っています。最初のセルにはUIImageView
が含まれ、2番目には2つ含まれています。私は、そのセルが1つか2つの画像を持っているかどうかによって、隠れているか関連するものを表示しています。セルに画像がない場合は、UIView
の両方が隠されていますが、それらはまだ設定されている制約の一部を形成していますので、結果のセルは72ピクセルの高さですが、
は私が高さ、サイズ変更可能なUITableViewCell
年代を作成するための唯一の方法は、ストーリーボードに制約を設定し、レイアウトの問題が発生しますオンザフライプログラム的制約の値をいじりことであると言われています。 (なし成功して)、次のようなアドバイスのいずれかが真であるかどうか私にはわからないので、私は試してみました:
が私
ResizableImageCell
で高さを制御私のUIView
のNSLayoutConstraint
年代に明示的な参照を設定します呼び出し、原因となるのはUnable to simultaneously satisfy constraints
です。そして私
ResizableImageCell
コールで高さを制御すべてNSLayoutConstraint
年代への明示的な参照を設定します。またはUIView
の両方を追加して削除すると、テーブルをスクロールして指を画面から持ち上げた後にテーブルが上下に飛び出し始めるまで正常に動作します。
これはViewController
を制御する、現在のコードです:
import UIKit
class ViewController: UIViewController
{
@IBOutlet weak var tableView: UITableView!
let imageNames = [ "room", "street", "tree" ]
override func viewDidLoad()
{
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0
}
func getRandomInt (from start: Int, to end: Int) -> Int
{
return Int(arc4random_uniform(UInt32((end + 1) - start))) + start
}
}
extension ViewController: UITableViewDataSource
{
@available(iOS 2.0, *)
public func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "ResizableImageCell") as! ResizableImageCell
let numImages = getRandomInt(from: 0, to: 2)
cell.view1.isHidden = numImages != 1
cell.view2.isHidden = numImages != 2
var rand = getRandomInt(from: 0, to: imageNames.count - 1)
switch numImages
{
case 1: cell.image1.image = UIImage(named: imageNames[ rand ])
case 2: cell.image2.image = UIImage(named: imageNames[ rand ])
default: ()
}
guard numImages == 2 else { return cell }
rand = getRandomInt(from: 0, to: imageNames.count - 1)
cell.image3.image = UIImage(named: imageNames[ rand ])
return cell
}
func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 64
}
}
これは私のオーダーメイドResizableImageCell
セル
import UIKit
class ResizableImageCell: UITableViewCell
{
@IBOutlet weak var view1: UIView!
@IBOutlet weak var view2: UIView!
@IBOutlet weak var image1: UIImageView!
@IBOutlet weak var image2: UIImageView!
@IBOutlet weak var image3: UIImageView!
override func awakeFromNib()
{
super.awakeFromNib()
}
}
そして、ここでは、既存のスクリーンショットで制御する、現在のコードですストーリーボード:
ありがとうございます。
ありがとうございます@チーフ - ルッツ、私はこれを試し、結果を知ってみましょう。 –
だから@ Chief-Lutz:まだ決定的なものはないが、あなたの提案がうまくいくように見える。ありがとう。 –
@ TheMotherofJosephBeuys - それが助けてくれることを願っています。あなたは大歓迎です:) –