TableViewCell 重叠文本
TableViewCell overlaps text
我的 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()
}
我过去的一个 UITableView 也遇到过类似的问题。有很多原因可能会导致这种情况,但也许我也遇到过同样的事情。
我看到您正在使用自定义 tableViewCell。可能发生的情况是,当您设置单元格的文本时,它会添加一个带有该文本的标签视图。现在假设你在 tableview 中滚动,那个单元格消失了。如果您要重用该单元格并且您没有从子视图中删除标签,或者将该标签的文本再次设置为所需的文本,您将重用带有先前标签的 tableviewcell 并添加带有新标签的新标签文本,重叠文本。
我的建议是确保您不会在 TimelineCell class 中继续添加 UIlabel 作为子视图,除非不存在标签。如果存在标签,则编辑该标签的文本而不是单元格的文本。
The table view’s data source implementation of
tableView:cellForRowAtIndexPath: should always reset all content when
reusing a cell.
看来你的问题是你不总是设置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 元素并附加到单元格,就好像您需要在回收单元格之前处理它一样。
您可以尝试设置:
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
的 UITableViewDelegate
希望对您有所帮助
我可以解决这个问题。在情节提要中,标签已取消 "Clears Graphics Context"。我检查了一下,现在它解决了!感谢您的帮助!
将单元格设置为 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
}
我的 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()
}
我过去的一个 UITableView 也遇到过类似的问题。有很多原因可能会导致这种情况,但也许我也遇到过同样的事情。
我看到您正在使用自定义 tableViewCell。可能发生的情况是,当您设置单元格的文本时,它会添加一个带有该文本的标签视图。现在假设你在 tableview 中滚动,那个单元格消失了。如果您要重用该单元格并且您没有从子视图中删除标签,或者将该标签的文本再次设置为所需的文本,您将重用带有先前标签的 tableviewcell 并添加带有新标签的新标签文本,重叠文本。
我的建议是确保您不会在 TimelineCell class 中继续添加 UIlabel 作为子视图,除非不存在标签。如果存在标签,则编辑该标签的文本而不是单元格的文本。
The table view’s data source implementation of tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell.
看来你的问题是你不总是设置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 元素并附加到单元格,就好像您需要在回收单元格之前处理它一样。
您可以尝试设置:
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
的 UITableViewDelegate
希望对您有所帮助
我可以解决这个问题。在情节提要中,标签已取消 "Clears Graphics Context"。我检查了一下,现在它解决了!感谢您的帮助!
将单元格设置为 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
}