2014-12-29 16 views
0

我创建了一个包含UIImage和一些标签的自定义UITableView单元。我已经将UIImage标记为100,并且一直试图将其更新如下:在UITableView中更新UIImage

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错误。

回答

1

您需要检查是否CellImageView不是零 只需添加一个if语句就像我在做代码。此发生在你,因为CellImageView是nil.If你检查的CellImageView为零时,你有什么都不会happen.So一个可选的使用它。我不知道你想要什么用viewWithTag.You做可以使用另一种观点来加载使用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. 
} 
1

只要使用本

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 weak var curentImage:UIImageView!

倍率FUNC viewDidLoad中(){ super.viewDidLoad()

var image = UIImage(named: currentPhoto!.filename) 
curentImage.image = image 



// Do any additional setup after loading the view. 

}

倍率FUNC didReceiveMemoryWarning(){ super.didReceiveMemoryWarning()

导向这个加载图像你在另一种观点。链接这个类与视图在IB

+0

我只是困惑,为什么我需要第二类?为什么我不能使用在界面构建器中生成的ID在第一个类中引用我的UIImageView?如果可以的话,我宁愿把所有东西都放在一个班级里。 – user3185748