简体   繁体   English

Swift-将UIVIewcontroller可变数组访问到UITableViewCell类

[英]Swift - Access UIVIewcontroller mutable array to UITableViewCell class

I have a UIViewController which contains the UITableView , Have created the separate class for UITableViewCell according to my logic I want to pass the array for every row. 我有一个包含UITableViewUIViewController ,根据我想为每行传递数组的逻辑,为UITableViewCell创建了单独的类。 So I have created one mutable array and assigning the new values whenever the cell for method called. 因此,无论何时调用方法的单元格,我都创建了一个可变数组并分配了新值。

But the mutable array I can't able to access from the UITableViewCell class 但是我无法从UITableViewCell类访问可变数组

I have used the below code 我用下面的代码

class PostPage: UIViewController{
    public var imageArray = [String] ()
}

extension PostPage: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if self.UserDatas?.posts.app[indexPath.row].images?.count ?? 0 > 0 {
            let cell:PostImageCell = tablView.dequeueReusableCell(withIdentifier: "PostImageCell") as! PostImageCell
            self.imageArray = self.UserDatas?.posts.app[indexPath.row].images ?? ["String URL"]
            cell.selectionStyle = .none
            return cell
        }
    }

    class PostImageCell: UITableViewCell{
        override func awakeFromNib() {
            super.awakeFromNib()
            var mainClass : ViewController = ViewController()
            let image = self.mainClass.imageArray
        }
    }
}

Here am I getting the error saying like "Value of type 'ViewController' has no member 'imageArray'" . 这是我收到的错误消息,例如“类型为'ViewController'的值没有成员'imageArray'”

Your code cannot work. 您的代码无法正常工作。

Do it the other way round and pass the array in cellForRow 反过来做,并在cellForRow传递数组

class PostImageCell: UITableViewCell {
    var imageArray = [String]()
}

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

   let cell = tableView.dequeueReusableCell(withIdentifier: "PostImageCell") as! PostImageCell
   cell.imageArray = self.UserDatas?.posts.app[indexPath.row].images ?? []
   cell.selectionStyle = .none
   return cell
}

First you have no VC named ViewController 首先,您没有名为ViewController VC

var mainClass : ViewController = ViewController()
let image = self.mainClass.imageArray

Second you either make the array global 其次,您要么使数组成为全局数组

class Service {
   static let shared = Service()
   var imageArray = [String] ()
}

Then use it like this inside all the app 然后在所有应用程序中像这样使用它

Service.shared.imagArray

Or create another one inside the cell custom class 或者在单元格自定义类中创建另一个

class PostImageCell: UITableViewCell{
      var imageArray = [String] ()
}

and assign it in cellForRowAt 并在cellForRowAt分配它

let cell = //
cell.imageArray = imageArray 

It's right! 这是正确的! "Value of type 'ViewController' has no member 'imageArray'" “类型'ViewController'的值没有成员'imageArray'”

PostPage has it!!! PostPage有它!!!

change this: var mainClass: ViewController = ViewController() 更改此内容: var mainClass: ViewController = ViewController()

to this: var mainClass: PostPage = PostPage() 为此: var mainClass: PostPage = PostPage()

That error will gone. 该错误将消失。

But remember: it's new initialized object and contains nothing by itself. 但是请记住:它是新的初始化对象,并且本身不包含任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM