具有单元格指示符的动态UITableCellView高度

时间:2014-11-05 12:32:15

标签: ios uitableview swift

这个问题与我解决的另一个问题有关: Dynamic UITableCellView height

我希望使用此代码实现动态单元格高度:

import UIKit

class MyTableViewController: UITableViewController {

    var entries:Array<String> = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        var i = 0
        while i < 20 {
            entries.append("\(i) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor")
            i++;
        }

        self.tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.entries.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any
        let cell = self.tableView.dequeueReusableCellWithIdentifier("basic_cell", forIndexPath: indexPath) as UITableViewCell

        var label = cell.viewWithTag(13)

        if let unwrappedLabel = label as? UILabel {
            unwrappedLabel.text = self.entries[indexPath.row]
        }

        return cell
    }
}

然而,当我将披露指标添加到每个单元格时

cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

我得到了这个结果:

enter image description here

左侧带指示器,右侧没有。

您可以查看此测试项目的github仓库:https://github.com/ArtworkAD/DynamicCellTest

任何想法都错了吗?我正在使用swift与xcode 6.1和iOS8.1

1 个答案:

答案 0 :(得分:1)

问题在于我的原始代码:

self.tableView.estimatedRowHeight = 64

这似乎打破了一切。删除后,一切都按预期工作。

相关问题