两个部分和不同的三个细胞

时间:2018-02-07 10:16:21

标签: swift uitableview

1.我还有一个section1

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return 1
    }else{
        return 3 //imageName.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var thirdCell = ThirdTableViewCell()
    var cell = UITableViewCell()
    var row2Cell = Row2TableViewCell()

    if indexPath.row == 0 {
        cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
        return cell
    }else if indexPath.row == 1{
        thirdCell = tableView.dequeueReusableCell(withIdentifier: "thirdCell", for: indexPath) as! ThirdTableViewCell
        thirdCell.image1.image = #imageLiteral(resourceName: "plus")//imageArray[indexPath.row]
        return thirdCell
    }else{
        row2Cell = tableView.dequeueReusableCell(withIdentifier: "Row2Cell", for: indexPath) as! Row2TableViewCell
        return row2Cell
    }
}

I have one more section1

2.我如何获得ThirdViewController.image

How did I get ThirdViewController

1 个答案:

答案 0 :(得分:0)

我认为您想要访问 ThirdTableViewCell类

中的 ThirdViewController.image

这是您可以在其他View Controller或自定义类中访问viewController的实例(例如Labels,Images和TextFeilds等)。

import UIKit

internal weak var AccessThirdViewController: ThirdViewController?

class ThirdViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var imageViewToChange: UIImageView!

    override func viewDidLoad() {
        AccessThirdViewController = self
    }

它可以在所有其他viewControllers或类中访问

import UIKit

class ThirdTableViewCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @IBAction func onButtonClick(_ sender: Any) {
        AccessThirdViewController?.imageViewToChange.image = UIImage(named: "rocket")!
    }


}

现在您只需使用 AccessThirdViewContoller

直接访问ThirdViewController
  

或者您可以使用NotificationCenter作为创建来实现此目的   NotificationCenter addObserver

收到通知的接收(获取)通知和函数方法处理程序:

import UIKit

class ThirdViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

     @IBOutlet weak var imageViewToChange: UIImageView!

     override func viewDidLoad() {

     NotificationCenter.default.addObserver(self, selector: #selector(setImage), name: Notification.Name("UpdateImage"), object: nil)
     }

     @objc func setImage(){
          self.imageViewToChange.image = UIImage(named: "rocket")
      }

然后从UIButton的@IBAction上的ThirdTableViewCell类发送(发布)通知

import UIKit

class ThirdTableViewCell: UITableViewCell {

        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }

        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)

            // Configure the view for the selected state
        }

        @IBAction func onButtonClick(_ sender: Any) {
            NotificationCenter.default.post(name: Notification.Name("UpdateImage"), object: nil)
       }


 }