简体   繁体   English

视图控制器中的两个表视图:Swift

[英]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.

相关问题 Swift 2.0在一个视图控制器cellForRowIndexPath中包含两个表视图 - Swift 2.0 two table views in one view controller cellForRowIndexPath 在集合视图控制器中快速移动两个视图 - Swift a two views within a collection view controller 如何通过根表视图控制器中的swift在两个表视图之间建立Segue连接? - How to establish Segue connection between two table views via swift in the root table view controller? 快速地将具有分层表视图的导航控制器放入视图控制器中 - Putting a navigation controller with hierarchical table views inside of a view controller in swift Swift 4 One View Controller两个视图隐藏导航栏 - Swift 4 One View Controller two views hide navigation bar 无法在一个视图控制器中加载两个表视图 - unable to load two table views in one view controller 快速加载两个集合视图和一个表视图的coredata - Loading coredata for two collection views and one table view in swift 不同视图控制器中的两个表视图一个工作一个不工作(Swift 4) - Two Table Views In Different View Controllers one working and one not(Swift 4) 一个视图控制器的两个视图 - Two views for a single view controller 如何通过基于选项卡的视图控制器快速管理5种不同的表视图(和详细视图)? - How to manage 5 different table views (and detail views) from a tab-based view controller in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM