TableViewCell重叠文字

7

我遇到了一个关于TableViewCell的问题

在我的storyboard中有两种类型的cell。当我滚动时,一些单元格中的文本会重叠。我尝试了所有方法,但我不知道还能怎么做。非常感谢您的帮助。

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()
    }

enter image description here


我能看到TimelineCell,其他类型在哪里? - Icaro
这种情况通常会发生在重用单元格并在单元格的行方法中不断添加子视图时。然而,我在您发布的代码中没有看到任何此类情况。是否有更多的代码? - Beau Nouvelle
@lcaro,另一种类型我不使用,因为现在我试图解决这个问题。 - Carolina
Beau,我没有添加子视图到单元格的代码。 - Carolina
@Carol,这个单元格里发生了什么?这段文本是来自两个不同的单元格吗?还是单元格中出现了错位? - Icaro
文本来自2个不同的单元格吗? - Carolina
5个回答

7

我可以解决这个问题。在故事板中,标签的“Clears Graphics Context”未被选中。我已经选中了它,现在问题已经得到解决了!感谢您的帮助!


0

我以前也遇到过UITableView的类似问题。有很多原因可能会导致这种情况,但也许是和我遇到的一样。

我看到你正在使用自定义的tableViewCell。可能发生的情况是,当你设置单元格的文本时,它会添加一个带有该文本的标签视图。现在假设你滚动浏览表格视图并且该单元格消失了。如果你要重用该单元格,并且你没有从子视图中删除标签,或者没有再次将该标签的文本设置为所需的文本,那么你将重复使用带有先前标签的tableviewcell,并向其添加一个新的带有新文本的标签,从而重叠文本。

我的建议是,在TimelineCell类中确保不要持续将UIlabels添加为子视图,除非不存在标签。如果存在标签,请编辑该标签的文本而不是单元格的文本。


嗨,泰勒。我没有在单元格中添加标签。我在故事板上有标签,只设置了文本。 - Carolina

0
根据苹果文档

表视图的数据源实现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 元素并将其附加到单元格中,因为如果这样做,您需要在回收单元格之前将其释放。

0

你可以尝试设置:

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 44.0 // Set this value as a good estimation according to your cells
}

在包含你的tableView的视图控制器中,
确保你的TimelineCell中的布局约束定义了一个清晰的高度约束线。
另一个选项是响应:
tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
 return 44.0 // Your height depending on the indexPath
}

始终在包含您的tableView并且我假设是tableViewUITableViewDelegate的ViewController中

希望这有所帮助


0

将单元格设置为nil,这将修复一些错误。 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

var cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? ImageCellTableViewCell
    cell = nil
    if cell == nil {
        cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for:indexPath) as? ImageCellTableViewCell
    }
    cell.yourcoustomtextTextLabel.text = "this is text"
    cell.yourcoustomtextImageView.image = image
    return cell
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接