2014-12-29 18 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