我创建了一个包含 UIImage 和一些标签的自定义 UITableView 单元格。我已将 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 错误。
最佳答案
您需要检查 CellImageView 是否为 nil
只需添加一个 if 语句,就像我在代码中所做的那样。这发生在你身上,因为 CellImageView 是 nil。如果你检查 CellImageView 是 nil,什么都不会发生。所以当你有一个可选的总是在使用它之前检查它。我不知道是什么你想用 viewWithTag 做。你可以使用另一个 View 来加载图像,使用 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.
}
关于ios - 更新 UITableView 中的 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27691803/