Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITableView中更新UIImage_Ios_Iphone_Uitableview_Swift_Uiimage - Fatal编程技术网

Ios 在UITableView中更新UIImage

Ios 在UITableView中更新UIImage,ios,iphone,uitableview,swift,uiimage,Ios,Iphone,Uitableview,Swift,Uiimage,我创建了一个自定义UITableView单元格,其中包含一个UIImage和一些标签。我已将UIImage标记为100,并尝试按如下方式更新它: import UIKit class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! let Items = ["Altitude","D

我创建了一个自定义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\u BAD\u指令错误。

您需要检查CellImageView是否为零 只需像我在代码中所做的那样添加if语句。这发生在您身上,因为CellImageView为nil。如果您检查CellImageView为nil,则不会发生任何事情。因此,当您有可选项时,请在使用它之前始终检查它。我不知道您想对viewWithTag执行什么操作。您可以使用另一个视图来加载图像,方法是使用
override func Prepareforsgue(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
类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弱var curentImage:UIImageView

重写func viewDidLoad(){ super.viewDidLoad()

}

重写函数didReceiveMemoryWarning(){ 超级。我收到了记忆警告()


这会将图像加载到另一个视图上。将该类与IB中的视图链接起来我只是不明白为什么我需要第二个类?为什么我不能使用在interface builder中生成的ID在第一个类中引用UIImageView?如果可以,我宁愿将所有内容都保存在一个类中。
class DisplayViewController: UIViewController {
var image = UIImage(named: currentPhoto!.filename)
curentImage.image = image



// Do any additional setup after loading the view.