2017-08-03 17 views
0

我的代碼如下列出了uitableview單元格(a和b)上的兩件事情。我想要一個gif在顯示「a」的單元格上顯示一次。如果else聲明依賴於數組元素(swift3)

import UIKit 

class ViewController: UIViewController { 


var i1 = ["a","b"] 
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return i1.count 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 


    cell.textLabel?.textAlignment = .center 
    cell.textLabel?.text = pets[indexPath.row] 


    //what I am trying to do but does not work. if the cell reads a I want the cell to display a image. 
     if i1[0] { 
      cell.imageView?.loadGif(name: "ftf") 
      } 


    return cell 
}} 
+0

你寫的「a」是什麼意思? –

回答

0

這裏

if i1[0] 

這行你是不是比較它與任何東西。

對於建立你有「A」爲第一指標,嘗試一下本作的比較

if i1[indexPath.row] == "a" { 
    cell.imageView?.loadGif(name: "ftf") 
} 
0

你怎麼沒有得到一個編譯器錯誤 - 字符串不是轉換爲bool?

在Swift中,您需要一個布爾型內部if-block。在你的情況下,是

if "a" == i1[indexPath.row] { 
} 
+1

我收到一個錯誤,我只是輸入我正在嘗試做的事情。 –

+0

我給你錯誤的原因。你需要在if塊中提供一個布爾值,在你的情況下你需要做字符串比較。 –