ios - Swift 中的 UITableView - 单元格出队

标签 ios uitableview swift xcode6

func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
    return 5
}

func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
        let cell = tableView.dequeueReusableCellWithIdentifier("BookCell") as BookTableViewCell

        println("ip: \(indexPath.row)")
        cell.bookLabel.text = "Row #\(indexPath.row)"

        return cell
    }

我只看到一个单元格,文本被覆盖了 5 次,而不是 5 个单元格。我以为我们不必再做 if (!cell) 废话了?我做错了什么?

最佳答案

如果您看到 UILabels 被覆盖了 5 次,那是因为您的单元格高度尚未定义并且计算为 0 像素高。以下任何一项都可以帮助您显式或隐式定义行高:

1) 在 Interface Builder 中设置行高:

enter image description here

2)实现UITableViewDataSource协议(protocol)方法:

func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    return 50.0
}

3) 在单元格的 UILabelUITableViewCell 的外边界之间设置自动布局约束:

enter image description here

关于ios - Swift 中的 UITableView - 单元格出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24836012/

相关文章:

ios - 如何计算不接触 UIView 的随机 CGPoint

ios - UITableViewCell 中的两个堆叠动态高度标签

ios - 如何将 UITableView 作为 UIView 的 subview 添加?

swift - 在 Realm、Swift 中按天从 NSDate 过滤

xcode - iOS Swift 选项卡栏图标插入在运行时被删除

ios - UIPicker 显示 ?而不是文字

javascript - react-native 中的 clonewithrows 数组问题

ios - 将 tableview 添加到 scrollview 并让 scrollview 在单元格添加到 tableview 时保持高度增长

ios - 自定义 TableViewCell Swift

ios - 如何使用 swift 在应用程序中调用一次计时器?