Ios 的值没有成员
我试图通过观看Youtube教程获得CustomCell,但我在Tableview上遇到了这个问题 “CustomCell”类型的值没有成员“mainImage”。我希望你们中的一些人能帮助我解决这个问题 我想在手机中获取的主要图像位于CustomCell.swift中 我的TableViewController:Ios 的值没有成员,ios,swift,Ios,Swift,我试图通过观看Youtube教程获得CustomCell,但我在Tableview上遇到了这个问题 “CustomCell”类型的值没有成员“mainImage”。我希望你们中的一些人能帮助我解决这个问题 我想在手机中获取的主要图像位于CustomCell.swift中 我的TableViewController: import UIKit struct CellData { let image : UIImage? let message : String? } var
import UIKit
struct CellData {
let image : UIImage?
let message : String?
}
var data = [CellData]()
class TableViewController : UITableViewController{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
data = [CellData.init(image: #imageLiteral(resourceName: "Bildschirmfoto 2018-09-20 um 22.17.11"), message: "The Avengers")]
self.tableView.register(CustomCell.self, forCellReuseIdentifier: "custom")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "custom") as! CustomCell
cell.mainImage = data[indexPath.row].image
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
<#code#>
}
}
您必须将子视图放入单元格类中:
class CustomCell: UITableViewCell {
var messageView: UITextView = {
var textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
var mainImageView: UIImageView = {
var imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
...
}
为什么要将所有这些变量声明为全局变量,而不是使它们成为类属性?
class CustomCell: UITableViewCell {
var messageView: UITextView = {
var textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
var mainImageView: UIImageView = {
var imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
...
}