[英]Two table views in view controller: Swift
I have a UITableView
with prototype cells that essentially takes up the whole page. 我有一个带有原型单元的UITableView
,它实际上占据了整个页面。 I have a UIButton
on the very bottom that should display a pop-up static UITableView
when tapped. 我在最底部有一个UIButton
,在点击时应显示一个弹出的静态UITableView
。 I'm struggling to account for the pop-up table view in cellForRowAtIndexPath
. 我正在努力考虑cellForRowAtIndexPath
的弹出表视图。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let result: UITableViewCell
if tableView == self.tableView {
var cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = array[indexPath.row]
result = cell
} else if tableView == self.popUpTableView {
var popUpCell = self.popUpTableView.dequeueReusableCellWithIdentifier("popUpCell", forIndexPath: indexPath) as! UITableViewCell
popUpCell.textLabel!.text = popUpArray[indexPath.row]
result = popUpCell
}
return result
}
I'm getting an error at return result
, where Variable 'result' used before being initialized
, but I'm declaring it at the very top. 我在return result
出错,在Variable 'result' used before being initialized
,但我在最上面声明了它。 Where am I going wrong with this? 我在哪里错呢?
You need to have exhaustive choices. 您需要有详尽的选择。 It's possible that result never gets initialized because your checks are "if" and "else if". 结果可能从未初始化,因为您的检查是“ if”和“ else if”。 What happens if tableView is not either "self.tableView" or "self.popUpTableView"? 如果tableView既不是“ self.tableView”也不是“ self.popUpTableView”,会发生什么?
The simple fix is (if you only plan on having these two) to simply change your "else if" to a simple "else". 简单的解决方法是(如果您仅打算同时拥有这两个)将“ else if”更改为简单的“ else”。 This way result will always get initialized. 这样,结果将始终被初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.