swift - 两个部分和不同的三个单元

标签 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

最佳答案

据我所知,您想在 ThirdTableViewCell 类 中访问 ThirdViewController.image

这是您在其他 View Controller 或自定义类中访问 viewController 实例(例如标签、图像和 TextFeilds 等)的方法。

import UIKit

internal weak var AccessThirdViewController: ThirdViewController?

class ThirdViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var imageViewToChange: UIImageView!

    override func viewDidLoad() {
        AccessThirdViewController = self
    }

它将使其可以在所有其他 viewController 或类中访问

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

Or you can achieve this by using NotificationCenter as creating 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)
       }


 }

关于swift - 两个部分和不同的三个单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48661165/

相关文章:

ios - UIAlertController 没有出现

ios - 如何在 Swift 中正确测试 Core Data

ios - 如何在 ios 中动态更改 uitableview 的特定标题部分高度

ios - 从 CMSampleBuffer 中提取数据以创建深拷贝

ios - Interface Builder 文件中的未知类 XXXXX

ios - 将两个变量传递给同一个 View Controller swift

ios - View Controller 之间的 Swift 传输数据保持为空

ios - 具有动态 UILabel 的 UITableViewCell 的动态高度

ios - 如何创建一个 View Controller 数组?

iphone - iOS 6 : UITableView Edit and Autoresizing