ios - View Controller 中的两个 TableView : Swift

标签 ios swift uitableview

我有一个 UITableView,其原型(prototype)单元格基本上占据了整个页面。我在最底部有一个 UIButton,点击时应该显示一个弹出式静态 UITableView。我正在努力解释 cellForRowAtIndexPath 中的弹出 TableView 。

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
    }

我在 return result 处遇到错误,其中 Variable 'result' used before being initialized,但我在最顶部声明​​了它。我哪里出错了?

最佳答案

您需要有详尽的选择。结果可能永远不会被初始化,因为您的检查是“if”和“else if”。如果 tableView 不是“self.tableView”或“self.popUpTableView”,会发生什么情况?

简单的解决方法是(如果您只打算拥有这两个)简单地将“else if”更改为简单的“else”。这样结果将始终得到初始化。

关于ios - View Controller 中的两个 TableView : Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31487175/

相关文章:

ios - 将 ARSCNView 场景背景内容设置为 iPhone X 相机

ios - 通过点击表格 View 中的单元格来选中/取消选中复选框,以及如何知道哪个单元格已选中或未选中

ios - 如何在 UITableViewCell 中垂直居中对齐 UIButton? ( objective-c )

ios - 如何在 ViewController 中随机化 3 张图像?

ios - 如何使用此代码在 tableview 中使用搜索栏?

ios - 同时以相同的速度将多个连续的 UILabels 动画到同一点

swift - 如何在 Swift 2.2 中替换复杂的 C 风格 for 循环

ios - 如何在 swift 查询返回时明智地从 HealthKits HKSample 中获取数据

string - 如何将 Swift 字符转换为字符串?

iphone - 是否 dequeueReusableCellWithIdentifier : call an initializer in my UITableViewCell subclass?