我有一个自定义的 UITableViewCell
并且它曾经工作正常,但我已经升级到 xcode 8 并且我仍在使用 Swift 2.3 并且才刚刚开始注意到这个问题,所以我'我不能 100% 确定它何时开始发生。
我正在配置一个单元格,它所做的只是制作一个圆形标签并在其中放置一个数字,旁边是一个名称。它应该看起来像这样:
但是,当屏幕最初加载时,圆形标签丢失了。如果我向下滚动 tableView 并且该行离开屏幕,当我向后滚动并且它再次成为焦点时,标签会出现。附件也是一样,如果我点击编辑行,然后返回,就会生成圆形标签。为什么它在初始加载时不起作用?
这是 UITableViewCell 的自定义代码:
func circleLabels(label: UILabel, hex: String){
label.layer.cornerRadius = label.frame.size.width / 2
label.layer.masksToBounds = true
}
func configureForPlayer(player: Player) {
playerName.text = player.name
jerseyLabel.text = String(player.jersey)
circleLabels(jerseyLabel, hex: "#6470FF")
}
CellForRowAtIndexPath
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PlayerCell", forIndexPath: indexPath) as! PlayerCell
let player = fetchedResultsController.objectAtIndexPath(indexPath) as! Player
cell.configureForPlayer(player)
return cell
}
最佳答案
我的猜测是第一次调用circleLabels
时label.frame
还没有设置。一旦绘制完成并且 label.frame
具有实际大小,代码就会按预期开始工作。如果我正确地放置了一个常量而不是 label.frame.size.width/2
应该可以解决这个问题。
关于ios - UITableViewCell 最初没有正确格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39947496/