今日、私は 6 時間以上かけて動的セルを作成しようとしましたが、うまくいきませんでした。私もいくつかの素晴らしいチュートリアルを見つけました。私は何か間違ったことをしていて、誰かが私に何を教えてくれることを望んでいます.
私のアプリ: テーブルは空で始まります。追加ボタンをクリックすると、2 つのテキスト入力フィールド (タイトル/説明) を持つモーダル VC が表示されます。そのデータはテーブルに戻されます。メイン VC のアンワインド メソッドは、テーブル データをリロードします。
問題: セルの説明部分が 1 行を超えると、途切れてしまう。
テーブルを格納するメイン ビュー コントローラーのコードは次のとおりです。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Initial Setup
//***** ----- ***** ------ ***** ----- ***** ----- *****
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Automatic table reload upon unwind
@IBAction func unwindToMain(segue: UIStoryboardSegue) {
//RW's code for dynamic cell height
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80.0
tableView.reloadData()
}
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Functions
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Swipe to delete task cell
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if(editingStyle == UITableViewCellEditingStyle.Delete) {
taskMgr.tasks.removeAtIndex(indexPath.row)
tableView.reloadData()
}
}
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Table View & Cell Setup
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Tells the table how many rows it should render
//*Looks to the taskMgr to count tasks, creates equal # of rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskMgr.tasks.count
}
//Creates the individual cells. If the above function returns 3, this runs 3 times
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "BasicCell")
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80.0
cell.textLabel?.text = taskMgr.tasks[indexPath.row].name
cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].desc
return cell
}
}
私のモーダル/エディター ビューのコード:
class EditorView: UIViewController, UITextFieldDelegate {
@IBOutlet var txtTask: UITextField!
@IBOutlet var txtDesc: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Keyboard Functionality
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Dismisses keyboard upon tapping the return key
func textFieldShouldReturn(textField: UITextField) -> Bool{
textField.resignFirstResponder()
return true
}
//Dismisses keyboard upon touch outside text boxes
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Code to run upon Editor being dismissed
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Actions performed upon tapping the 'Finish' button
@IBAction func dismissEditorView(sender: AnyObject) {
//Calls a previously written function to append textfield input to the table array
taskMgr.addTask(txtTask.text, desc: txtDesc.text)
//Dismisses the EditorView
dismissViewControllerAnimated(true, completion: nil)
}
}
新しいアイテムをテーブルに追加する機能を収容する私のタスクマネージャーファイル:
import UIKit
//This has global scope!!
var taskMgr: TaskManager = TaskManager()
struct task {
var name = "Name TBD"
var desc = "Desc TBD"
}
class TaskManager: NSObject {
var tasks = [task]()
func addTask(name: String, desc: String){
tasks.append(task(name: name, desc: desc))
}
}
私が参照しているチュートリアル:
http://natashatherobot.com/ios-8-self-sizing-table-view-cells-with-dynamic-type/
http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift
http://coding.tabasoft.it/ios/ios8-self-sizing-uitableview-cells/
http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html
私が正しくやっていると思うこと:
- ビューにテーブルを追加、テーブルにプロトタイプ セルを追加
-セルに 2 つのラベルが追加されました。自動レイアウトが追加されました。トップ ラベルの制約はコンテナー (先頭、末尾、および上部) に対するものです。下の制約は 2 番目のラベルです。ボトム ラベルには、上から 2 番目のラベルがあり、次に先頭、末尾、および下のコンテナーがあります。
・トップレーベルは耐圧縮性と内容物抱き込みの優先度751。下のラベルは、4 つすべてに対して 750 の優先度があります。
-両方のラベルの優先幅が自動に設定されています。明示的はチェックされていません