【问题标题】:Update UIImage in UITableView更新 UITableView 中的 UIImage
【发布时间】:2014-12-29 15:40:44
【问题描述】:

我创建了一个自定义 UITableView 单元格,其中包含一个 UIImage 和一些标签。我已将 UIImage 标记为 100 并尝试按如下方式对其进行更新:

import UIKit

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!


let Items = ["Altitude","Distance","Groundspeed"]

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.Items.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    cell.textLabel?.text = self.Items[indexPath.row]

    var CellImageView: UIImageView? = cell.viewWithTag(100) as UIImageView?
    CellImageView!.image = UIImage(named: "Airspeed")

    return cell
}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

我应该如何更新 UIImage?我目前在运行我的应用程序时收到 EXC_BAD_INSTRUCTION 错误。

【问题讨论】:

    标签: ios iphone uitableview swift uiimage


    【解决方案1】:

    您需要检查 CellImageView 是否不为零 只需添加一个 if 语句,就像我在代码中所做的那样。这发生在你身上,因为 CellImageView 是 nil。如果你检查 CellImageView 是 nil,什么都不会发生。所以当你有一个可选的时候,总是在使用它之前检查它。我不知道是什么你想用 viewWithTag 来做。你可以使用另一个视图来加载图像,使用 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?).ex

    这就是我在项目中的做法

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        var secondeScene = segue.destinationViewController  as DisplayViewController
        // Pass the selected object to the new view controller.
        if let indexPath = self.tableView.indexPathForSelectedRow(){
            let selectedPhoto = photos[indexPath.row]
            secondeScene.currentPhoto = selectedPhoto
        } 
    

    import UIKit
    
    class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    
    
    let Items = ["Altitude","Distance","Groundspeed"]
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.Items.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
        cell.textLabel?.text = self.Items[indexPath.row]
    
        var CellImageView: UIImageView? = cell.viewWithTag(100) as UIImageView?//CellImageView is an optional
     if  CellImageView !== nil{//checking for CellImageView
        CellImageView!.image = UIImage(named: "Airspeed")
        }
        return cell
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    【讨论】:

      【解决方案2】:

      就用这个

      import UIKit
      

      类 SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

      @IBOutlet weak var tableView: UITableView!
      
      let image = UIImage(named: "Airspeed")
      let Items = ["Altitude","Distance","Groundspeed"]
      
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return self.Items.count
      }
      
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
          var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
          cell.textLabel?.text = self.Items[indexPath.row]
      
          var CellImageView: UIImageView? = cell.viewWithTag(100) as UIImageView?
          //if  CellImageView !== nil{
          //CellImageView!.image = UIImage(named: "Airspeed")
          //}
          return cell
      }
      
      
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
          self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
      }
      
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
          // Get the new view controller using [segue destinationViewController].
          var secondeScene = segue.destinationViewController  as DisplayViewController
          // Pass the selected object to the new view controller.
          if let indexPath = self.tableView.indexPathForSelectedRow(){
              let selectedPhoto = image
              secondeScene.currentPhoto = selectedPhoto
      
      
          }
      

      }

      这是第一个视图,然后将其用于第二个视图

      class DisplayViewController: UIViewController {
      

      var currentPhoto : UIImage?

      @IBOutlet 弱变量curentImage:UIImageView!

      覆盖 func viewDidLoad() { super.viewDidLoad()

      var image = UIImage(named: currentPhoto!.filename)
      curentImage.image = image
      
      
      
      // Do any additional setup after loading the view.
      

      }

      覆盖函数 didReceiveMemoryWarning() { super.didReceiveMemoryWarning()

      这会将您的图像加载到另一个视图中。将此类与 IB 中的视图链接

      【讨论】:

      • 我只是很困惑为什么我需要第二节课?为什么我不能使用我在界面生成器中生成的 ID 在第一类中引用我的 UIImageView?如果可以的话,我宁愿把所有东西都放在一个班级里。
      猜你喜欢
      • 2014-12-28
      • 2012-12-31
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多