自定义uiview在表格内部自定义单元格“跳跃”在表负载上
我有一个 uitaiteView
,其中内部的自定义单元格被称为 newscell
。
在 newscell
内,我有一个自定义 uiview
用作操作按钮。
很多时候,当 tableView
加载单元格时,自定义操作按钮出现在单元格的 top 持续几秒钟,直到它回到其正确的位置细胞的底部。有时,只有在我将单元滚出视口滚出后,它才能返回到正确的位置。
知道为什么会发生这种情况吗?
这是一张使事情变得更清晰的照片: contion按钮
使用.xib文件创建了newscell
protocol NewsCellDelegate {
func favoriteIconDidPress(forArticle article: Article)
func actionButtonDidPress(inside article: Article)
}
enum ArticleFavoriteMark {
case selected
case notSelected
}
class NewsCell: UITableViewCell, MainActionButtonDelegate{
@IBOutlet weak var entireNewsCell: UIView!
@IBOutlet weak var newsImage: UIImageView!
@IBOutlet weak var favoriteIcon: UIImageView!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var subjectTag: UIButton!
@IBOutlet weak var moreSubjectsTag: UIButton!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var authorLabel: UILabel!
@IBOutlet weak var summaryLabel: UILabel!
@IBOutlet weak var actionButton: MainActionButtonView!
var delegate: NewsCellDelegate?
var articleUrl = ""
var articleID = ""
var articleImageUrl = ""
var isFavorite: Bool = false
override func awakeFromNib() {
super.awakeFromNib()
setCellBorder()
setCellColorsDesign()
setImageRounded()
setTagsRounded()
setGestureRecognizer()
actionButton.delegate = self
}
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0))
}
func setGestureRecognizer() {
favoriteIcon.addGestureRecognizer(UITapGestureRecognizer(target: favoriteIcon, action: #selector(favoriteIconPressed)))
favoriteIcon.isUserInteractionEnabled = true
let tapGestureRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(favoriteIconPressed(tapGestureRecognizer:)))
favoriteIcon.addGestureRecognizer(tapGestureRecognizer1)
}
@objc func favoriteIconPressed(tapGestureRecognizer: UITapGestureRecognizer) {
let currentArticle = Article(id: articleID, articleTitle: titleLabel.text!, date: dateLabel.text ?? "", url: articleUrl, content: summaryLabel.text!, author: authorLabel.text ?? "", topic: subjectTag.currentTitle!, imageUrl: articleImageUrl , isFavorite: isFavorite)
delegate?.favoriteIconDidPress(forArticle: currentArticle)
}
func actionButtonDidPress(btnText: String) {
let currentArticle = Article(
id: articleID,
articleTitle: titleLabel.text ?? "",
date: dateLabel.text ?? "",
url: articleUrl,
content: summaryLabel.text ?? "",
author: authorLabel.text ?? "",
topic: subjectTag.currentTitle ?? "",
imageUrl: articleImageUrl,
isFavorite: self.isFavorite)
delegate?.actionButtonDidPress(inside: currentArticle)
}
}
newscell 是我的 newscell
元素层次结构:
元素我在 newscell
中的“操作”按钮上放了约束:
这就是我如何限制 actionbutton
uiview
:
在“动作”按钮自定义Uiview 内部的约束
在我的tableView中,我在tableView中启动了每个单元格,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let currentCell = tableView.dequeueReusableCell(withIdentifier: Constants.TableCellsIdentifier.FAVORITES, for: indexPath) as! SavedArticleCell
currentCell.delegate = self
let currentKey = viewModel.keysArray[indexPath.row]
let savedArticle = viewModel.savedArticles[currentKey]
if let savedArticle = savedArticle {
currentCell.articleID = savedArticle.id!
currentCell.articleTitle.text = savedArticle.title
currentCell.articleTopic.setTitle(savedArticle.topic, for: .normal)
if let imageUrl = savedArticle.imageUrl {
currentCell.articleImageURL = imageUrl
currentCell.articleImage.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "light-gray-background"))
} else {
currentCell.articleImage.image = UIImage(named: "light-gray-background")
}
}
return currentCell
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须添加
contentView.layoutifneeded()
inlayoutsubviews()
newscell
layoutifneed()
的目的是要强制一种视图及其子视图的同步(本质上是“立即”的)。您必须称呼此功能,因为您已经更新了layoutsubviews()
方法中的布局。关于layoutifneed()
这是说。You have to add
contentView.layoutIfNeeded()
inlayoutSubViews()
method ofNewsCell
The purpose of
layoutIfNeeded()
is to force a synchronous (essentially "immediate") redraw of a view and its subviews. You have to call this because you have updated the layout inlayoutsubviews()
method. RegardinglayoutIfNeeded()
Here is what Apple says.