簡體   English   中英

帶有自定義單元格的TableViewController

[英]TableViewController with custom cells in swift

我在Swift中使用自定義TableViewCell遇到問題。 我正在嘗試在TableViewCell中顯示自定義TableView但是效果不佳。 我在屏幕上看到一個灰色方塊...我在Internet上找到了一些建議禁用自動布局的答案。 我這樣做了,但是現在顯示了我的單元格,但是單元格的高度混亂了。 如您所見,本應具有紅色背景的單元格顯示在3個單元格的高度上,而背景僅顯示在1個單元格上。 單元格的高度和行高度都設置為150px。

           With Auto-Layout:                   Without Auto-Layout:

在此處輸入圖片說明在此處輸入圖片說明

這是單元格代碼:

class CharacterDescription : UITableViewCell {
    @IBOutlet var imageProfile: UIImageView!
    @IBOutlet var name: UILabel!
    @IBOutlet var hp: UILabel!
    @IBOutlet var damages: UILabel!
    @IBOutlet var range: UILabel!
    @IBOutlet var mp: UILabel!
    @IBOutlet var critChances: UILabel!
    @IBOutlet var critModifier: UILabel!

    required init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
    }
}

TableViewController代碼:

class CharacterDescriptionsViewController : UITableViewController, UITableViewDataSource, UITableViewDelegate {
    var userProfile: UserProfile!

    required init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)
    }

    override init() {
        super.init()
    }

    override func viewDidLoad() {
        var nibName = UINib(nibName: "CharacterDescription", bundle:nil)

        self.tableView.registerNib(nibName, forCellReuseIdentifier: "CharacterDescription")
    }

    override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
        return userProfile.characters!.count
    }


    override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
        var cell = self.tableView.dequeueReusableCellWithIdentifier("CharacterDescription", forIndexPath: indexPath) as CharacterDescription

        //!TODO: Init of cell
        return cell
    }
}

您必須重寫TableView.heightForRowAtIndexPath才能返回自定義單元格的高度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM