TableViewCell重叠文本

时间:2015-06-15 23:26:27

标签: ios xcode swift uitableview overlap

我的TableViewCell存在问题

我的故事板中有两种类型的单元格。 当我滚动时,文本在某些单元格中重叠。我尝试了一切,但我不知道该怎么办。非常感谢你的帮助

interface IDao<TEntity> where TEntity : IEntity
{
    TEntity Save(TEntity ent);
    IQueryable<TEntity> GetAll();
    bool Delete(TEntity ent, out String errMsg);
    void SaveChanges();
}

enter image description here

5 个答案:

答案 0 :(得分:7)

我可以解决这个问题。在故事板中,标签没有“清除图形上下文”。我查了一下,现在它解决了!谢谢你的帮助!

答案 1 :(得分:0)

过去我的UITableViews有一个类似的问题。有许多事情可能会导致这种情况,但也许发生在我身上的事情也是如此。

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

我的建议是确保您不会在TimelineCell类中添加UIlabel作为子视图,除非不存在标签。如果存在标签,则编辑该标签的文本而不是单元格。

答案 2 :(得分:0)

根据apple documentation

  

表视图的数据源实现   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元素并附加到单元格,就好像您需要在回收单元格之前处置它一样。

答案 3 :(得分: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的ViewController中,我认为是tableView的{​​{1}}

希望这有帮助

答案 4 :(得分:0)

将单元格设置为nil将修复一些错误。         func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - &gt;的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
}
相关问题