ios - 更新 UITableView 中的 UIImage

标签 ios iphone uitableview swift uiimage

我创建了一个包含 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/

相关文章:

iphone - 使用 Storyboard时,是否允许我使用 presentViewController :animated:completion:? 呈现 View Controller

ipad - 如何为 iPad 创建 YouTube 或 eBay 风格的界面?

ios - 用于核心数据搜索的 NSPredicate

ios - iPhone 无法正确完成应用内交易 (IOS7)

ios - 如何在我的 iPad 应用程序中通过不寻常的曲线路径为一张图像制作动画?

objective-c - 如何判断 NSString 是否为空

swift - 静态 UITableView 未显示在 UIViewController 中

ios - 如果它们在表中,我如何使用 hitTest 获取底层 View ?

ios - Instagram WebView 阻止 FB 登录

ios - XCode - 添加带有选项卡样式的按钮?