NSIndexPath?在Swift中没有成员名称'row'错误 [英] NSIndexPath? does not have a member name 'row' error in Swift
本文介绍了NSIndexPath?在Swift中没有成员名称'row'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用Swift语言和方法创建一个UITableViewController
I'm creating a UITableViewController with Swift language and in a method
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?
我收到此错误
NSIndexPath?在Swift中没有成员名称'row'错误
NSIndexPath? does not have a member name 'row' error in 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?
Then, how can set the cell.text with the array dataStore element?
推荐答案
你可以打开可选的 indexPath
带的参数如果让...
:
if let row = indexPath?.row {
cell.textLabel.text = self.dataStore[row]
}
或者如果您确定 indexPath
不是 nil
,你可以用强制解包!
:
or if you're sure indexPath
isn't nil
, you can force the unwrapping with !
:
cell.textLabel.text = self.dataStore[indexPath!.row]
请记住 indexPath!
关于nil值将是一个运行时异常,所以更好的做法是un如第一个例子所示包装它。
Just keep in mind that indexPath!
on a nil value will be a runtime exception, so it's better practice to unwrap it as in the first example.
这篇关于NSIndexPath?在Swift中没有成员名称'row'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文