Ios 除非缓存图像,否则TableView单元格格式不正确
我的tableview有一个问题,在缓存图像之前,单元格的方向不正确,只有当我返回页面并缓存图像时,单元格的方向才正确。这是我所说的一个例子,第一个图像是我第一次进入页面时的图像,我有一个函数将图像存储在imagecache中,第二个图像是当我在其他地方导航后返回该页面时的图像被缓存 格式不正确: 格式正确: 我知道这是一个非常微妙的区别 从外表看 但我很想弄明白这一点,明白为什么会发生这种情况 我的单元格恒定高度为85,以下是相关代码:Ios 除非缓存图像,否则TableView单元格格式不正确,ios,swift,uitableview,image-caching,Ios,Swift,Uitableview,Image Caching,我的tableview有一个问题,在缓存图像之前,单元格的方向不正确,只有当我返回页面并缓存图像时,单元格的方向才正确。这是我所说的一个例子,第一个图像是我第一次进入页面时的图像,我有一个函数将图像存储在imagecache中,第二个图像是当我在其他地方导航后返回该页面时的图像被缓存 格式不正确: 格式正确: 我知道这是一个非常微妙的区别 从外表看 但我很想弄明白这一点,明白为什么会发生这种情况 我的单元格恒定高度为85,以下是相关代码: func tableView(tableView:U
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
//关于这一点,索引部分我遗漏了dw
让cell=discountTableView.dequeueReusableCell(标识符:self.discountCellId,for:indexath)作为!DiscountCell
cell.textLabel?.text=折扣.retailerName
cell.detailTextLabel?.text=discount.matchedFirstName+“”+discount.matchedLastName
cell.profileImageView.image=UIImage.gif(名称:“imageLoading”)!
grabImageWithCache(折扣.retailerImageLink){(profilePic)在中
cell.profileImageView.image=profilePic
}
//添加第二个图像
cell.matchImageView.image=UIImage.gif(名称:“imageLoading”)!
grabImageWithCache(discount.matchedProfileImage){(matchProfilepic)在中
cell.matchImageView.image=matchProfilepic
}
//声明单元格没有选择样式(避免灰色高亮显示)
cell.selectionStyle=.none
//设置单元格曲率的格式
cell.layer.cornerRadius=38
返回单元
}
**我很好奇为什么缓存图像时单元格的高度会发生变化,因为高度设置为常量,所以我不知道为什么会发生变化。当我打印单元格的高度时,它说两次都是85
UITableViewCell类:
类折扣单元格:UITableViewCell{
覆盖func布局子视图(){
super.layoutSubviews()
//单元格之间的垂直间距
让padding=UIEdgeInsets(顶部:0,左侧:0,底部:7,右侧:0)
bounds=bounds.inset(by:padding)
textlab?.frame=CGRect(x:120,y:textlab!.frame.origin.y-10,宽度:textlab!.frame.width,高度:textlab!.frame.height)
detailTextLabel?.frame=CGRect(x:120,y:detailTextLabel!.frame.origin.y,宽度:detailTextLabel!.frame.width,高度:detailTextLabel!.frame.height)
//单元格间距
让边距=UIEDGEINSET(顶部:0,左侧:0,底部:85,右侧:0)
contentView.frame=contentView.frame.inset(by:margins)
}
//设置占位符imageview
让profileImageView:UIImageView={
让imageView=UIImageView()
imageView.image=UIImage(名为:“失败”)
imageView.TranslatesAutoResizezingMaskintoConstraints=false
imageView.layer.cornerRadius=35
imageView.layer.masksToBounds=true
imageView.contentMode=.ScaleSpectFill
imageView.clipsToBounds=true
imageView.layer.borderWidth=2
imageView.layer.borderColor=#colorLiteral(红色:0.9725490196,绿色:0.9725490196,蓝色:0.9725490196,alpha:1)
返回图像视图
}()
//匹配的占位符图像视图
让matchImageView:UIImageView={
让imageView=UIImageView()
imageView.image=UIImage(名为:“失败”)
imageView.TranslatesAutoResizezingMaskintoConstraints=false
imageView.layer.cornerRadius=35
imageView.layer.masksToBounds=true
imageView.contentMode=.ScaleSpectFill
imageView.clipsToBounds=true
返回图像视图
}()
重写init(样式:UITableViewCell.CellStyle,reuseIdentifier:String?){
super.init(样式:。副标题,reuseIdentifier:reuseIdentifier)
添加子视图(匹配图像视图)
添加子视图(profileImageView)
//设置剖面Pic锚固件
profileImageView.leftAnchor.constraint(equalTo:self.leftAnchor,常量:5)。isActive=true
profileImageView.centerYAnchor.constraint(等式:self.centerYAnchor).isActive=true
profileImageView.widthAnchor.constraint(equalToConstant:70).isActive=true
profileImageView.heightAnchor.constraint(equalToConstant:70).isActive=true
//设置火柴锚
matchImageView.centerYAnchor.constraint(等式:self.centerYAnchor).isActive=true
matchImageView.widthAnchor.constraint(equalToConstant:70).isActive=true
matchImageView.heightAnchor.constraint(equalToConstant:70).isActive=true
matchImageView.leftAnchor.constraint(equalTo:profileImageView.leftAnchor,常量:35)。isActive=true
}
必需初始化?(编码器:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
}
我相信我已经缩小了这个问题的范围,我注意到两次单元格之间的间距都不一致,所以我认为问题在于这些代码行
//单元格之间的垂直间距
让padding=UIEdgeInsets(顶部:0,左侧:0,底部:7,右侧:0)
bounds=bounds.inset(by:padding)
我不知道该怎么做,因为我看了一些教程说要更改contentviews插图,但当我这样做时,它没有任何效果,我看到一些东西说iOS 11改变了你这样做的方式,但还没有找到如何真正着手解决这个问题。你的细胞是一个药丸形状,这意味着您可以通过执行
cell.layer.cornerRadius=cell.frame.height/2
来实现相同的效果。接下来,您应该对profileIm使用顶部和底部约束
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, constant: -5.0),
//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, constant: 5),
// 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, constant: 0.0),
// Leading 12 pts from Match image Trailing
mainLabel.leadingAnchor.constraint(equalTo: matchImageView.trailingAnchor, constant: 12.0),
// 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, constant: 0.0),
subLabel.trailingAnchor.constraint(equalTo: mainLabel.trailingAnchor, constant: 0.0),
])
}
}
// 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, constant: 20.0),
discountTableView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
discountTableView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
])
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
}
}