【发布时间】:2023-03-31 22:51:01
【问题描述】:
我正在使用 Swift 语言和方法创建一个 UITableViewController
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?
我收到了这个错误
NSIndexPath?在 Swift 中没有成员名称“行”错误
我不明白为什么。
这是我的代码
import UIKit
class DPBPlainTableViewController: UITableViewController {
var dataStore: NSArray = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
self.dataStore = ["one","two","three"]
println(self.dataStore)
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return self.dataStore.count
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel.text = self.dataStore[indexPath.row]
return cell
}
}
那么,如何用数组dataStore元素设置cell.text呢?
【问题讨论】:
-
“我不明白为什么”因为
NSIndexPath?不是NSIndexPath。请记住,Optional 是一个枚举包装“真实”值。要访问该值,您需要打开它。 -
行和节的 NSIndexPath 扩展在 UIKit 中的 UITableView.h 文件中进行了扩展。
标签: ios swift uitableview