2016-06-08 57 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