1

我有问题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()

}
4

1 回答 1

0

viewDidLoad is not passed an IndexPath parameter like cellForRowAt is.

IndexPath is used to identify either the current cell to be built or selected. You do not need it in viewDidLoad. You have 4 converters in the array and can select any you like whenever you need to just by using its index.

于 2018-07-05T08:20:12.300 回答