如何解决除非缓存了图像,否则TableView Cell的格式不正确
我的表格视图出现问题,即在缓存图像之前,单元格无法正确定向,并且只有当我返回页面并缓存了图像时,它们才能正确定向。这是我正在谈论的示例,第一个图像是我第一次访问该页面时,并且具有将图像存储在图像缓存中的功能,第二个图像是我在导航至其他位置后返回该页面时,并且图像被缓存。
格式错误:
格式正确:
我知道这是非常微妙的区别 与它的外观 但我很想弄清楚这一点,并理解为什么会发生这种情况
我的单元格恒定高度为85,这是相关代码:
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Indexing part I left out dw about this
let cell = discountTableView.dequeueReusableCell(withIdentifier: self.discountCellId,for: indexPath) as! DiscountCell
cell.textLabel?.text = discount.retailerName
cell.detailTextLabel?.text = discount.matchedFirstName + " " + discount.matchedLastName
cell.profileImageView.image = UIImage.gif(name: "imageLoading")!
grabImageWithCache(discount.retailerImageLink) { (profilePic) in
cell.profileImageView.image = profilePic
}
//Adding second Image
cell.matchImageView.image = UIImage.gif(name: "imageLoading")!
grabImageWithCache(discount.matchedProfileImage) { (matchProfilepic) in
cell.matchImageView.image = matchProfilepic
}
//declare no selection style for cell (avoid gray highlight)
cell.selectionStyle = .none
//format the cell's curvature
cell.layer.cornerRadius = 38
return cell
}
**我很好奇为什么缓存图像时单元格的高度似乎会发生变化,因为高度被设置为一个常数,所以我不知道为什么它会发生变化。当我打印单元格的高度时,它说也是两次85 ...
UITableViewCell类:
class DiscountCell: UITableViewCell {
override func layoutSubviews() {
super.layoutSubviews()
//vertical spacing between cells
let padding = UIEdgeInsets(top: 0,left: 0,bottom: 7,right: 0)
bounds = bounds.inset(by: padding)
textLabel?.frame = CGRect(x: 120,y: textLabel!.frame.origin.y-10,width: textLabel!.frame.width,height: textLabel!.frame.height)
detailTextLabel?.frame = CGRect(x: 120,y: detailTextLabel!.frame.origin.y,width: detailTextLabel!.frame.width,height: detailTextLabel!.frame.height)
//spacing between cells
let margins = UIEdgeInsets(top: 0,bottom: 85,right: 0)
contentView.frame = contentView.frame.inset(by: margins)
}
//sets a placeholder imageview
let profileImageView: UIImageView = {
let imageView = UIImageView ()
imageView.image = UIImage(named: "failed")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.cornerRadius = 35
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.layer.borderWidth = 2
imageView.layer.borderColor = #colorLiteral(red: 0.9725490196,green: 0.9725490196,blue: 0.9725490196,alpha: 1)
return imageView
}()
//PlaceHolder imageview for match
let matchImageView:UIImageView = {
let imageView = UIImageView ()
imageView.image = UIImage(named: "failed")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.cornerRadius = 35
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
return imageView
}()
override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
super.init(style: .subtitle,reuseIdentifier: reuseIdentifier)
addSubview(matchImageView)
addSubview(profileImageView)
//Setting Profile Pic anchors
profileImageView.leftAnchor.constraint(equalTo: self.leftAnchor,constant: 5).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 70).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 70).isActive = true
//Setting Match Anchors
matchImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
matchImageView.widthAnchor.constraint(equalToConstant: 70).isActive = true
matchImageView.heightAnchor.constraint(equalToConstant: 70).isActive = true
matchImageView.leftAnchor.constraint(equalTo: profileImageView.leftAnchor,constant: 35).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我相信我已经解决了这个问题,我注意到两次单元格之间的间隔不一致,所以我认为问题在于这些代码行
//vertical spacing between cells
let padding = UIEdgeInsets(top: 0,right: 0)
bounds = bounds.inset(by: padding)
我不确定该怎么做,因为我看到教程说要更改contentviews实例,但是当我这样做时,它没有效果,而且我看到一些事情说iOS 11改变了您这样做的方式,但是并没有能够找到实际解决此问题的方法。
解决方法
情侣笔记...
修改单元格的bounds
或其contentView
并不是一个好主意。 UIKit将它们用于很多事情(例如在实现表编辑时调整内容)。
单元格子视图应添加到单元格的contentView
,而不是单元格本身。同样,由于表视图如何依赖该层次结构。
您可以使用UIView
和UIImageView
子类来为您处理“四舍五入”。例如:
class RoundImageView: UIImageView {
override func layoutSubviews() {
layer.cornerRadius = bounds.size.height * 0.5
}
}
class RoundEndView: UIView {
override func layoutSubviews() {
layer.cornerRadius = bounds.size.height * 0.5
}
}
这是一个完整的实现:
class RoundImageView: UIImageView {
override func layoutSubviews() {
layer.cornerRadius = bounds.size.height * 0.5
}
}
class RoundEndView: UIView {
override func layoutSubviews() {
layer.cornerRadius = bounds.size.height * 0.5
}
}
class DiscountCell: UITableViewCell {
// "Holder" view... contains all other UI elements
// use RoundEndView so it handles the corner radius by itself
let holderView: RoundEndView = {
let v = RoundEndView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .white
return v
}()
//sets a placeholder imageview
// use RoundImageView so it handles the corner radius by itself
let profileImageView: RoundImageView = {
let imageView = RoundImageView()
imageView.image = UIImage(named: "failed")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.layer.borderWidth = 2
imageView.layer.borderColor = #colorLiteral(red: 0.9725490196,green: 0.9725490196,blue: 0.9725490196,alpha: 1)
return imageView
}()
//PlaceHolder imageview for match
// use RoundImageView so it handles the corner radius by itself
let matchImageView: RoundImageView = {
let imageView = RoundImageView()
imageView.image = UIImage(named: "failed")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
return imageView
}()
let mainLabel: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.font = UIFont.systemFont(ofSize: 17.0,weight: .regular)
return v
}()
let subLabel: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.font = UIFont.systemFont(ofSize: 14.0,weight: .regular)
return v
}()
override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
super.init(style: .subtitle,reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
backgroundColor = .clear
contentView.addSubview(holderView)
holderView.addSubview(matchImageView)
holderView.addSubview(profileImageView)
holderView.addSubview(mainLabel)
holderView.addSubview(subLabel)
NSLayoutConstraint.activate([
// holder view constraints Top 5 pts from contentView Top
holderView.topAnchor.constraint(equalTo: contentView.topAnchor,constant: 5.0),// Leading 12 pts from contentView Leading
holderView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor,constant: 12.0),// Trailing 5 pts from contentView Trailing
holderView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor,constant: -5.0),// 5 pts from contentView Bottom (use lessThanOrEqualTo to avoid auto-layout warnings)
holderView.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor,//Setting Profile Pic anchors - Top and Leading 5 pts from Top and Leading of Holder view
profileImageView.topAnchor.constraint(equalTo: holderView.topAnchor,constant: 5),profileImageView.leadingAnchor.constraint(equalTo: holderView.leadingAnchor,// Botom 5 pts from contentView Bottom - this will determine the height of the Holder view
profileImageView.bottomAnchor.constraint(equalTo: holderView.bottomAnchor,constant: -5),// width 70 pts,height equal to width (keeps it "round")
profileImageView.widthAnchor.constraint(equalToConstant: 70.0),profileImageView.heightAnchor.constraint(equalTo: profileImageView.widthAnchor),//Setting Match Pic Anchors - 35 pts from Profile Leading,centerY to Profile
matchImageView.leadingAnchor.constraint(equalTo: profileImageView.leadingAnchor,constant: 35),matchImageView.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor,constant: 0.0),// same width and height as Profile
matchImageView.widthAnchor.constraint(equalTo: profileImageView.widthAnchor),matchImageView.heightAnchor.constraint(equalTo: profileImageView.heightAnchor),//Setting Main Label Anchors - Top equal to Top of Match image
mainLabel.topAnchor.constraint(equalTo: matchImageView.topAnchor,// Leading 12 pts from Match image Trailing
mainLabel.leadingAnchor.constraint(equalTo: matchImageView.trailingAnchor,// prevent Trailing from going past holder view Trailing
mainLabel.trailingAnchor.constraint(equalTo: holderView.trailingAnchor,constant: -16.0),//Setting Sub Label Anchors - Top 8 pts from Main label Bottom
subLabel.topAnchor.constraint(equalTo: mainLabel.bottomAnchor,constant: 8.0),// Leading and Trailing equal to Main label Leading / Trailing
subLabel.leadingAnchor.constraint(equalTo: mainLabel.leadingAnchor,subLabel.trailingAnchor.constraint(equalTo: mainLabel.trailingAnchor,])
}
}
// example Discount object struct
struct Discount {
var retailerName: String = ""
var matchedFirstName: String = ""
var matchedLastName: String = ""
var matchedProfileImage: String = ""
var retailerImageLink: String = ""
}
class DiscountViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
let discountTableView = UITableView()
let discountCellId = "dcID"
var myData: [Discount] = []
override func viewDidLoad() {
super.viewDidLoad()
// a little sample data
var d = Discount(retailerName: "Cup & Cone",matchedFirstName: "Kara",matchedLastName: "Tomlinson",matchedProfileImage: "pro1",retailerImageLink: "")
myData.append(d)
d = Discount(retailerName: "Cup & Cone",matchedFirstName: "Sophie",matchedLastName: "Turner",matchedProfileImage: "pro2",retailerImageLink: "")
myData.append(d)
d = Discount(retailerName: "Another Retailer",matchedFirstName: "WanaB3",matchedLastName: "Nerd",matchedProfileImage: "pro3",retailerImageLink: "")
myData.append(d)
discountTableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(discountTableView)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
discountTableView.topAnchor.constraint(equalTo: g.topAnchor,constant: 20.0),discountTableView.leadingAnchor.constraint(equalTo: g.leadingAnchor,discountTableView.trailingAnchor.constraint(equalTo: g.trailingAnchor,constant: -20.0),discountTableView.bottomAnchor.constraint(equalTo: g.bottomAnchor,])
view.backgroundColor = UIColor(red: 1.0,green: 0.9,blue: 0.7,alpha: 1.0)
discountTableView.backgroundColor = UIColor(red: 0.66,green: 0.66,blue: 0.9,alpha: 1.0)
discountTableView.separatorStyle = .none
discountTableView.delegate = self
discountTableView.dataSource = self
discountTableView.register(DiscountCell.self,forCellReuseIdentifier: discountCellId)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return myData.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Indexing part I left out dw about this
let discount = myData[indexPath.row]
let cell = discountTableView.dequeueReusableCell(withIdentifier: self.discountCellId,for: indexPath) as! DiscountCell
cell.mainLabel.text = discount.retailerName
cell.subLabel.text = discount.matchedFirstName + " " + discount.matchedLastName
// add first image
if let img = UIImage(named: "imageLoading") {
cell.profileImageView.image = img
}
// use your custom image funcs
//cell.profileImageView.image = UIImage.gif(name: "imageLoading")!
//grabImageWithCache(discount.retailerImageLink) { (profilePic) in
// cell.profileImageView.image = profilePic
//}
//Adding second Image
if let img = UIImage(named: discount.matchedProfileImage) {
cell.matchImageView.image = img
}
// use your custom image funcs
//cell.matchImageView.image = UIImage.gif(name: "imageLoading")!
//grabImageWithCache(discount.matchedProfileImage) { (matchProfilepic) in
// cell.matchImageView.image = matchProfilepic
//}
//declare no selection style for cell (avoid gray highlight)
cell.selectionStyle = .none
return cell
}
}
结果:
,您的细胞呈药丸状,这意味着您可以通过cell.layer.cornerRadius = cell.frame.height / 2
来达到相同的效果。接下来,您应该为profileImageView
使用顶部和底部约束,以便它强制单元格具有特定的填充,而不是尝试自己创建填充。问题是,当缓存图像时,单元格的高度有些变化,不知道是什么原因造成的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。