ios - 无法从 didSelectRowAtIndexPath 引用 cellForRowAtIndexPath 中的 IBoutlet

标签 ios uitableview swift

我设置了一个表格,其中包含单元格的图片。然后我希望当用户单击单元格时图片消失并显示一些标签。但是,每当我尝试从 didSelectRowAtIndexPath 引用 IBoutlets 时,它们都会返回 nil。我在这里做错了什么?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    //setting up the table cell to populate the Model Data
    var moCell:modelCell = self.tableView.dequeueReusableCellWithIdentifier("moCell") as! modelCell

    photo[indexPath.row].getDataInBackgroundWithBlock{
        (imageData: NSData?, error: NSError?) -> Void in

        if error == nil {

            let modelImage = UIImage(data: imageData!)

            moCell.modelPhoto.image = modelImage

        }

    }

    return moCell

}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var moCell:modelCell = self.tableView.dequeueReusableCellWithIdentifier("moCell") as! modelCell


    moCell.top.text = modelsInRange[indexPath.row]
    moCell.middle.text = "\(genNumber[indexPath.row]) Generation"
    moCell.bottom.text = "\(startYear[indexPath.row]) - \(endYear[indexPath.row])"

}

最佳答案

您正在 didSelectRowAtIndexPath 上调用 dequeueReusableCellWithIdentifier(id),因此您不会获得选定的单元格。

我会修改您的cellForRowAtIndexPath 实现并重新加载表格数据。在您的单元格子类上标记一些标志,以便您可以检查并在 cellForRowAtIndexPath 上相应地设置它。或者更好的是,只需重新加载该单个单元格。

我所说的一个非常快速的例子是:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    //setting up the table cell to populate the Model Data
    var moCell:modelCell = self.tableView.dequeueReusableCellWithIdentifier("moCell") as! modelCell

    if (photo[indexPath.row].isSelected)
    {
        moCell.top.text = modelsInRange[indexPath.row]
        moCell.middle.text = "\(genNumber[indexPath.row]) Generation"
        moCell.bottom.text = "\(startYear[indexPath.row]) - \(endYear[indexPath.row])"

    } else
    {
        photo[indexPath.row].getDataInBackgroundWithBlock{
            (imageData: NSData?, error: NSError?) -> Void in

            if error == nil {
                let modelImage = UIImage(data: imageData!)
                moCell.modelPhoto.image = modelImage
        }
    }
    return moCell
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    photo[indexPath.row].isSelected = YES
    self.tableView.reloadData()
}

关于ios - 无法从 didSelectRowAtIndexPath 引用 cellForRowAtIndexPath 中的 IBoutlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29830318/

相关文章:

ios - 有没有办法使用 Geometry Reader 将我的 SwiftUI 图像的宽度定义为相对于屏幕尺寸?

swift - 如何使用 NSTimer 从 10 秒开始倒计时然后停止计时器?

swift - 为什么要使用可选绑定(bind)?

IOS objective-C 仅在 ViewController 可见时执行 GCD

ios - UITableView 在未分配给父级 UIViewController View 属性时不滚动

ios - 在 UITableView 中设置多个部分

ios - 如何从 parentViewController 中的 childViewController 更改 UITextField 和 UISwitch 的值

ios - "auto"和 "instancetype"类型的主要区别是什么?

ios - 设置以编程方式绘制的 View 的颜色

ios - iPad屏幕右半部分的模态视图?