2016-06-08 64 views
-1

我有一个TableView和基于数组的单元格。现在我想改变一个变量的基础上巫婆的数组显示在单元格中。UITableViewCell的内容

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 

    } 

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

+0

我相信你正在寻找 开关Cell.textLabel?.text –

+0

你能详细说明你正在努力完成什么吗?你指的是什么论点和内容? Swift不支持你使用的对象“string”语法。另外,我建议不要使用大写变量名称,但这是另一个话题。 –

+0

谢谢,但我曾尝试过,并没有工作,它给了我的错误代码'字符串'不能成为'字符串?'的成员。 – PascalCzasny

回答

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 

这里的一切所需要的代码:

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 
    } 
} 
+0

我尝试了所有三种方法,但总是显示我不工作 – PascalCzasny

+0

我不知道为什么它不适用于您,您可能做了其他的事情,所以我添加了一个需要的所有代码的示例。我只是测试它,它完美的作品。 http://s33.postimg.org/vpqcs323z/Simulator_Screen_Shot_Jun_8_2016_12_32_21_PM.png –

+0

不,它显示一切都不起作用 – PascalCzasny