UITableViewCell的内容

问题描述 投票:-1回答:1

我有一个TableView和一个基于数组的Cell。现在我想根据单元格中显示的数组输入更改变量。

var array = ["first", "second", "third"]
var result = String

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let Cell = self.tableView.dequeueReusableCellWithIdentifier("CellID", forIndexPath: indexPath) as UITableViewCell

        Cell.textLabel?.text = array[indexPath.row]

// and here I need the content of the Cell

        switch Cell."content argument" {
            case "first":
                var result = "works"
            case "second":
                var result = "works also"
            case "third":
                var result = "what a surprise, works also"
            default:
                var result = "doesn't work"
        }  

        return Cell

    }

在这里我需要参数来获取Cell的内容。并且,没有“你必须创建一个新的文件或功能或扩展”,不,只是请参考!

swift uitableview switch-statement
1个回答
0
投票

你基于Cell.textLabel?.text切换时遇到的问题是textLabel是Optional,所以它的文字也是OptionalOptional是一个有两个案例的枚举,.Some.None,这就是为什么你的案子不起作用。

有两种可能的解决方案。

  1. 将您的开关放在if let声明中: Cell.textLabel?.text = array[indexPath.row] if let text = Cell.textLabel?.text { switch text { case "first": var result = "works" case "second": var result = "works also" case "third": var result = "what a surprise, works also" default: var result = "doesn't work" } }
  2. 不要根据单元格的内容进行切换,而是根据非可选数据进行切换: let text = array[indexPath.row] Cell.textLabel?.text = text switch text { case "first": var result = "works" case "second": var result = "works also" case "third": var result = "what a surprise, works also" default: var result = "doesn't work" }

然后就是你要制作大量未使用的局部变量的问题,这些变量都被命名为结果。所以让我们更好地使它成为常量并将其设置在开关中,并可能将结果添加到单元格中,如下所示:

    let text = array[indexPath.row]

    let result: String
    switch text {
        case "first":  result = "works"
        case "second": result = "works also"
        case "third":  result = "what a surprise, works also"
        default:       result = "doesn't work"
    }

    Cell.textLabel?.text = text
    Cell.detailTextLabel?.text = result

Here's all the code needed:

import UIKit

class TableViewController: UITableViewController {
    var array = ["first", "second", "third"]

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        let text = array[indexPath.row]
        let result: String
        switch text {
        case "first":  result = "works"
        case "second": result = "works also"
        case "third":  result = "what a surprise, works also"
        default:       result = "doesn't work"
        }

        cell.textLabel?.text = text
        cell.detailTextLabel?.text = result
        return cell
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.