我有问题indexPath.row
。在cellForRowAt
这行里面工作完美:
let cell = tableView.dequeueReusableCell(withIdentifier: "MenuViewCell", for: indexPath) as! MenuViewCell
let item = converters[indexPath.row]
cell.ConverterName.text = item.converterName
cell.converterImage.image = item.converterImg
但是当我尝试在 viewdidload 中实现它时出现错误:“使用未解析的标识符'indexPath'”
和代码:
struct converter {
let converterName: String
let converterImg: UIImage
}
var converters = [converter(converterName: "Converter of time", converterImg: #imageLiteral(resourceName: "time")),
converter(converterName: "Converter of pressure", converterImg: #imageLiteral(resourceName: "davlenie")),
converter(converterName: "Converter of speed", converterImg: #imageLiteral(resourceName: "skorost")),
converter(converterName: "Converter of distance", converterImg: #imageLiteral(resourceName: "dlinna"))]
override func viewDidLoad() {
super.viewDidLoad()
let item = converters[indexPath.row]
let itemSort = item.converterName
self.converters = itemSort.sorted { $0 < $1 }
self.tableView.reloadData()
}