テーブルのすべての行に、そのセルのラベル内のテキストの行に基づいて行の高さを設定しようとしています。出発の私のポイントは、ここでWenderlichチュートリアルです: https://www.raywenderlich.com/129059/self-sizing-table-view-cellsUITableViewAutomaticDimensionを使用した後、すべて同じ高さになる
あなたは以下を参照することができますように私は方法UITableViewAutomaticDimension
を使用しています。しかし何らかの理由で私がシミュレーションを実行すると、行はすべて同じ高さになります。ストーリーボードのテーブルビューセルで、行の高さを見積もってコードの行の高さを入力するか、行の高さを入力するかは関係ありません。常にすべての行に対して同じ高さになります。
私はここで何が欠けていますか?みんなありがとう!
import UIKit
class LoLFirstTableViewController: UITableViewController {
var tasks:[Task] = taskData
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
@IBAction func cancelToLoLFirstTableViewController(_ segue:UIStoryboardSegue) {
}
@IBAction func saveAddTask(_ segue:UIStoryboardSegue) {
if let AddTaskTableViewController = segue.source as? AddTaskTableViewController {
if let task = AddTaskTableViewController.task {
tasks.append(task)
let indexPath = IndexPath(row: tasks.count-1, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath)
as! TaskCell
let task = tasks[indexPath.row] as Task
cell.task = task
return cell
}
}
`
セルを表示するには、間違った自動レイアウト制約が必要です。 –
セルとセルのサイズが異なるコンテンツがありますか? *「ストーリーボードのテーブルビューセルで、行の高さを見積もっている行の高さや、行の高さに入力した数字の数は問題にならない」*自動サイジングセルの最終的な高さを決定する際には、これらのものはどちらも重要ではありません。 – Connor
はい、コンテンツのサイズはセルごとに異なる必要があります。そこに私のサンプルテキストは、1行から3行に及ぶ。私は自分の制約のスクリーンショットとシミュレータの出力が現在どのようなものかを付け加えています。ありがとうUmairとConnor! –