【问题标题】:if else statement depending on array element (swift3)if else 语句取决于数组元素(swift3)
【发布时间】:2017-08-03 14:47:14
【问题描述】:

我下面的代码列出了 uitableview 单元格(a 和 b)上的 2 件事。我希望在写入“a”的单元格上只显示一次 gif。

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

【问题讨论】:

  • 你写的“a”是什么意思?

标签: ios arrays uitableview swift3


【解决方案1】:

这一行

if i1[0]

你没有把它和任何东西比较。

对于以“a”作为第一个索引的设置,请尝试此进行比较

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

【讨论】:

    【解决方案2】:

    你怎么没有收到编译器错误 - String is not convertible to Bool?

    在 Swift 中,if-block 内部需要一个布尔值。在你的情况下是

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

    【讨论】:

    • 我收到一个错误,我只是输入我想要做的事情。
    • 我已经给你错误的原因了。您需要在 if-block 中提供一个布尔值,在您的情况下,您需要进行字符串比较。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    相关资源
    最近更新 更多