简体   繁体   中英

if else statement depending on array element (swift3)

My code below lists 2 things on a uitableview cell (a and b). I want a gif to be display just once on the cell where "a" is written.

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

This line here

if i1[0]

You aren't comparing it to anything.

For the set up you have with "a" as the first index, try this for the comparison

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

How are you not getting a compiler error- String is not convertible to Bool?

In Swift you need a boolean inside if-block . In your case that is

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM