【问题标题】:Swift 4 TableView Cell not Displaying correct imagesSwift 4 TableView Cell 不显示正确的图像
【发布时间】:2018-03-13 02:01:12
【问题描述】:

当我滚动 tableView 时,UIImageView 没有显示正确的图像。如何在 swift 4 语言中正确完成此操作。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

        //Transform Data From ^ to load at the bottom
        tableView.transform = CGAffineTransform (scaleX: 1,y: -1);
        cell?.contentView.transform = CGAffineTransform (scaleX: 1,y: -1);
        cell?.accessoryView?.transform = CGAffineTransform (scaleX: 1,y: -1);


        let username = cell?.viewWithTag(1) as! UITextView
        username.text = messageArray[indexPath.row].username

        let message = cell?.viewWithTag(2) as! UITextView
        message.text = messageArray[indexPath.row].message

        let timeStamp = cell?.viewWithTag(3) as! UILabel
        timeStamp.text = messageArray[indexPath.row].timeStamp

        let imageView = cell?.viewWithTag(4) as! UIImageView
        let urlString = messageArray[indexPath.row].photoUrl
        imageView.layer.cornerRadius = 10
        imageView.clipsToBounds = true

        //Load profile image(on cell) with URL & Alamofire Library
        let downloadURL = NSURL(string: urlString!)
        imageView.af_setImage(withURL: downloadURL! as URL)

        return cell!
    }

【问题讨论】:

标签: ios swift uitableview alamofire


【解决方案1】:

在 cellForRowAtIndexPath 方法的开头设置imageView.image = nil。您正在重用单元格。有时您需要重置单元组件。

【讨论】:

    【解决方案2】:

    您的解决方案:由于 cell 是可重复使用的,因此您需要在每次 cell for 调用时更新图像。

    最佳实践:我认为您可以以更好的方式进行此思考,并避免强制展开。

    1. 使用SDWebCache [https://github.com/rs/SDWebImage] pod 进行图像下载和缓存,因为您每次通过网络请求调用相同的图像。
    2. 使用Model的方式将数据与cell绑定。

    细胞类

    import UIKit
    import SDWebCache
    
    public struct TableCellModel {
        var username: String
        var imageURL: String
        var messsage: String
        var timeStamp: String
    }
    
    class TableCell: UITableViewCell {
    
        @IBOutlet weak var username: UITextView!
        @IBOutlet weak var message: UITextView!
        @IBOutlet weak var timeStamp: UITextView!
        @IBOutlet weak var imageView: UIImageView!
    
        override func awakeFromNib() {
            super.awakeFromNib()
        }
    
        var item: Any? {
            didSet {
                self.configure(item)
            }
        }
    
        func configure(_ item: Any?) {
            if let model = item as? TableCellModel {
                self.username.text = model.username
                self.message.text = model.message
                self.timeStamp.text = model.timeStamp
                if let url = URL(model.imageURL) {
                    self.imageView.sd_setImageWithURL(url)
                }
            }
        }
    }
    
    1. 将此类分配给情节提要中 tableview 单元格的自定义类
    2. 在你的视图控制器中声明var cellItems: [TableCellModel] = []
    3. viewDidLoad()like中初始化数组

    在视图控制器中:在viewDidLoad()中调用此方法

    func setupData() {
            self.cellItems =  [TableCellModel(username: "ABC", imageURL: "www....", message: "your message", timeStamp: "22.33.2222" ),
                                TableCellModel(username: "ABC", imageURL: "www....", message: "your message", timeStamp: "22.33.2222" ),
                                TableCellModel(username: "ABC", imageURL: "www....", message: "your message", timeStamp: "22.33.2222" ),
                                TableCellModel(username: "ABC", imageURL: "www....", message: "your message", timeStamp: "22.33.2222" )]
    
    self.tableView.reloadData()
    }
    

    和tableViewcellforRow委托一样

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableCell
        cell.item = self.cellItems[indexpath.row]
        return cell
    }
    

    【讨论】:

      【解决方案3】:

      我们在这里使用可重复使用的单元格,它确实reuses 带有旧图像的单元格。然后每次单元格显示在cellForRowAtIndexPath时都需要更新:

       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! MenuTableViewCell
              //MARK:-  Show the image
              let imgName = "menu_\(indexPath.row + 1)"
              //MARK:- Disable the UITableView selection highlighting
              cell.selectionStyle = .none
              return cell
          }
      

      您可以将您的图像设置为您的Assets.xcassetslike this 或任何您想要的格式-

      【讨论】:

      • 每个用户都有一个独特的图像这是正确的做法吗?
      • 您是否为用户使用任何 id?
      • 这不是我要找的。​​span>
      • 我没有使用 1 张图片。每张图片都是不同的,被查询到一个字符串。
      • first..你的用户有id吗?
      猜你喜欢
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      相关资源
      最近更新 更多