0

今日、私は 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 の優先度があります。

-両方のラベルの優先幅が自動に設定されています。明示的はチェックされていません

4

3 に答える 3

0

私の問題を解決しました。基本的に、プロトタイプ セルのタイトルと説明ラベルは、IBOutlets を使用してコードにリンクされていませんでした。アプリを実行してテーブル項目を追加すると、デフォルトのタイトルとサブタイトルが使用されたため、アプリは機能しました。ただし、それらには自動レイアウト制約または 0 に設定された行がなかったため、高さは静的に動作しました。

カスタム クラス ファイルを追加し、セルのカスタム クラスを新しいクラスとして設定し、IBOutlet をこのファイルにリンクしました。したがって、そのファイルは次のようになります。

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet var nameLabel:UILabel!
    @IBOutlet var descLabel:UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

次に、メインのViewControllerコードをわずかに変更しました(上記の質問に示されています。変更箇所は次のとおりです。

//Creates the individual cells. If the above function returns 3, this runs 3 times
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "BasicCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell

    cell.nameLabel.text = taskMgr.tasks[indexPath.row].name
    cell.descLabel.text = taskMgr.tasks[indexPath.row].desc

そしてそれだけでした。アイテムを追加するたびに、すべてが適切にサイズ調整されます。

于 2015-08-05T03:03:14.967 に答える
0

rowHeight と EstimatedRowHeight を使用しないことをお勧めします。それは機能しますが、セルがスクロールされたり、ビューが消えたりすると問題が発生し、すべてのセルがめちゃくちゃになります。すべての問題は、セルの高さが原因です。私は同じに答えましたが、次のリンクのiosで

IOS - セルフ サイジング セルの問題

これを迅速に変換してみてください。スイフトの知識がありません。お役に立てば幸いです。

于 2015-08-05T04:45:37.600 に答える
0

問題は、UITableViewCell が作成された後に.rowHeightandを設定していることです。.estimatedRowHeight次の行を に移動しますviewDidLoad()

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80.0
于 2015-08-03T17:38:54.213 に答える