簡體   English   中英

視圖控制器中的兩個表視圖:Swift

[英]Two table views in view controller: Swift

我有一個帶有原型單元的UITableView ,它實際上占據了整個頁面。 我在最底部有一個UIButton ,在點擊時應顯示一個彈出的靜態UITableView 我正在努力考慮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
    }

我在return result出錯,在Variable 'result' used before being initialized ,但我在最上面聲明了它。 我在哪里錯呢?

您需要有詳盡的選擇。 結果可能從未初始化,因為您的檢查是“ if”和“ else if”。 如果tableView既不是“ self.tableView”也不是“ self.popUpTableView”,會發生什么?

簡單的解決方法是(如果您僅打算同時擁有這兩個)將“ else if”更改為簡單的“ else”。 這樣,結果將始終被初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM