无法从另一个类调用对象

时间:2017-09-01 19:28:08

标签: ios swift class object

我有一个扩展单元格的表视图。扩展单元格来自xib文件。在表的类中,所有代码都控制着plist的扩展和提取数据。我正在尝试添加一个关闭按钮,但只希望它在细胞扩展时显示。就目前而言,我无法引用该按钮来隐藏它,因为它在另一个类中。以下是我尝试访问它的方式:

import UIKit

class SecondPolandViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!

    var customTableViewCell:CustomTableViewCell? = nil
    var items = [[String:String]]()

    override func viewDidLoad() {
        super.viewDidLoad()

        **REFERENCING CLASS** 

        customTableViewCell = CustomTableViewCell()

        let nib = UINib.init(nibName: "CustomTableViewCell", bundle: nil)
        self.tableView.register(nib, forCellReuseIdentifier: "cell")

        self.items = loadPlist()
    }

    func loadPlist()->[[String:String]]{
        let path = Bundle.main.path(forResource: "PolandResourceList", ofType: "plist")

        return NSArray.init(contentsOf: URL.init(fileURLWithPath: path!)) as! [[String:String]]
    }

    var selectedIndex:IndexPath?
    var isExpanded = false

    func didExpandCell(){
        self.isExpanded = !isExpanded
        self.tableView.reloadRows(at: [selectedIndex!], with: .automatic)
    }
}

extension SecondPolandViewController:UITableViewDataSource, UITableViewDelegate{
    ***HIDING BUTTON***

    let button = customTableViewCell?.closeButton
    button?.isHidden = true

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.selectedIndex = indexPath
        self.didExpandCell()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell

        cell.selectionStyle = .none

        let item = self.items[indexPath.row]
        cell.titleLabel.text = item["title"]
        cell.shortLabel.text = item["short"]
        cell.otherImage.image = UIImage.init(named: item["image"]!)
        cell.thumbImage.image = UIImage.init(named: item["image"]!)
        cell.longLabel.text = item["long"]

        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let height = UIScreen.main.bounds.height

        if isExpanded && self.selectedIndex == indexPath{
            //return self.view.frame.size.height * 0.6
            return 400
        }

        return 110
        //return height * 0.2
    }
}

但这并不隐藏它。

如果有帮助的话,这是我打电话的xib。这可能很简单,我只是一个新近自学的开发人员。

import UIKit

class CustomTableViewCell: UITableViewCell {
    @IBOutlet weak var closeButton: UIImageView!
    @IBOutlet weak var otherImage: UIImageView!
    @IBOutlet weak var thumbImage: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var shortLabel: UILabel!
    //@IBOutlet weak var longLabel: UITextView!
    @IBOutlet weak var longLabel: UITextView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        //let width = UIScreen.main.bounds.width
        //let height = UIScreen.main.bounds.height
        //thumbImage.frame.size.width = height * 0.19
        //thumbImage.frame.size.height = height * 0.19
    }
}

How it is now with circle that should be hidden. It's just an image not a button

enter image description here

2 个答案:

答案 0 :(得分:0)

正常的iOS答案是委托,但在这种情况下你可以通过一个简单的闭包来解决。

CustomTableViewCell中,添加

public var closeTapped: ((CustomTableViewCell) -> ())?

然后在该课程中,当点击关闭时,请致电

self.closeTapped?(self)

在VC中,在cellForRowAt中,

cell.closeTapped = { cell in 
    // do what you want with the VC
}

对于代表来说,这可能有所帮助:https://medium.com/@jamesrochabrun/implementing-delegates-in-swift-step-by-step-d3211cbac3ef

为什么更喜欢代表而不是关闭代码的快速答案是它将一组这些组合在一起的方便方法。这就是UITableViewDelegate(你正在使用它)。此外,这是一个常见的iOS习语。

我在这里写到:https://app-o-mat.com/post/how-to-pass-data-back-to-presenter用于类似情况(VC到VC通信)

答案 1 :(得分:0)

您似乎只需将这些行添加到cellForRowAt:indexPath方法中:

if indexPath == selectedIndexPath {
  cell.closeButton.isHidden = false
} else {
  cell.closeButton.isHidden = true
}

您可以在return

之前添加它们
相关问题