我的TableViewCell有问题
我的故事板上有两种类型的细胞。当我滚动时,文本在某些单元格中重叠。我什么都试过了,但我不知道怎么做。非常感谢你的帮助
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var storeNew = systemsBlog.getStore(listNews[indexPath.row].getIdStore())
var newNotice = listNews[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("TimelineCell", forIndexPath: indexPath) as? TimelineCell
cell!.nameLabel.text = storeNew.getName()
cell!.postLabel?.text = newNotice.getText()
cell!.postLabel?.numberOfLines = 0
cell!.dateLabel.text = newNotice.getDate()
cell!.typeImageView?.tag = indexPath.row;
return cell!
}
class TimelineCell : UITableViewCell {
@IBOutlet var nameLabel : UILabel!
@IBOutlet var postLabel : UILabel?
@IBOutlet var dateLabel : UILabel!
override func awakeFromNib() {
postLabel?.font = UIFont(name: "Roboto-Thin", size: 14)
}
override func layoutSubviews() {
super.layoutSubviews()
}

发布于 2015-06-16 04:13:40
我能解决这个问题。在故事板中,标签有"Clears上下文“。我查过了,现在解决了!谢谢你的帮助!
发布于 2015-06-16 03:22:20
过去,我的一个UITableViews也有类似的问题。有很多事情可能会导致这件事,但也许是同样的事情发生在我身上。
我看到您使用的是自定义tableViewCell。可能发生的情况是,当您设置单元格的文本时,它会添加带有该文本的label视图。现在,假设您在表视图中滚动,该单元格将消失。如果要重用该单元格,并且没有从子视图中删除标签,或者再次将该标签的文本设置为所需的文本,则将重用带有先前标签的tableviewcell,并向其添加一个新的标签,并将其与文本重叠。
我的建议是,除非不存在标签,否则不要一直将UIlabels作为子视图添加到TimelineCell类中。如果标签存在,请编辑该标签的文本,而不是单元格的文本。
发布于 2015-06-16 03:33:28
根据苹果文档
表视图的tableView:cellForRowAtIndexPath:的数据源实现应始终在重用单元格时重置所有内容。
似乎您的问题是,您不总是设置postLabel,导致它在其他单元格上写入,请尝试如下:
//reuse postLabel and set to blank it no value is returned by the function
let cell = tableView.dequeueReusableCellWithIdentifier("TimelineCell", forIndexPath: indexPath) as? TimelineCell
cell!.nameLabel.text = storeNew.getName()
if newNotice.getText() != nil{
cell!.postLabel.text = newNotice.getText()
} else {cell!.postLabel.text = ''}
cell!.postLabel.numberOfLines = 0
cell!.dateLabel.text = newNotice.getDate()
cell!.typeImageView?.tag = indexPath.row;
return cell!
}
//Make postLabel mandatory and set the font details in the xcode
class TimelineCell : UITableViewCell {
@IBOutlet var nameLabel : UILabel!
@IBOutlet var postLabel : UILabel!
@IBOutlet var dateLabel : UILabel!
override func awakeFromNib() {
//set this in xcode
}
override func layoutSubviews() {
super.layoutSubviews()
}还要确保您没有创建任何UI元素并附加到单元格中,就好像您需要在回收单元之前将其释放一样。
https://stackoverflow.com/questions/30856586
复制相似问题