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
}
}
2.如何获取ThirdViewController.image
最佳答案
据我所知,您想在 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/