【问题标题】:Swift: Bool if true, doesn't display imageSwift:布尔如果为真,不显示图像
【发布时间】:2016-02-23 08:38:03
【问题描述】:

我是初学者,请耐心解释,谢谢。

所以,基本上我在 parse 中有一个 bool 列,如果它是假的,我想显示一个图像,如果它是真的不显示任何东西。

这是我的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("todayCell", forIndexPath: indexPath) as! reqTodaysCell      
        let cellDataParse: PFObject = self.dataparse.objectAtIndex(indexPath.row) as! PFObject

        var newReadQuery = PFQuery(className: "request")
        newReadQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
            if let objects = objects {
                for object in objects {

                    if object["reqRead"] as! Bool == true {

                        myCell.isRead.image = nil //here is where I say pic to be nil but what happens is that if the first one is true then it will remove the pic for all of them.
        // and if its not true it should display the pic

                    } else {

        myCell.isRead.image = UIImage(named: "newReq")
                        print("user not read")

                    }


                }
            }
        })

如果我解释不正确,请告诉我,我会尽力再次解释。

【问题讨论】:

    标签: swift parse-platform tableviewcell


    【解决方案1】:

    这听起来像是三元运算符的理想用例。根据我下面的示例,您使用 ? : 跟在 Bool 后面的语法,如果 bool 为真则返回第一种情况,如果为假则返回第二种情况。

    newReadQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
            if let objects = objects {
                for object in objects {
    
                  let reqRead = object["reqRead"] as! Bool
    
                  cell.image.image = reqRead ? nil : UIImage(named: "newReq")
    
                }
            }
        })
    

    更新

    上述方法可能不起作用,因为在加载单元格之前可能未完成 Parse 调用。

    创建一个全局变量(在任何函数之外):

    var reqRead = [Bool]()
    

    在 ViewDidLoad 中,您可以创建一个布尔数组。

        var newReadQuery = PFQuery(className: "request")
     newReadQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
        if let objects = objects {
    
            for object in objects {
    
              reqRead.append(object["reqRead"] as! Bool)
    
            }
          tableView.reloadData()
        }
    })
    

    然后在您的 CellForRow 中:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("todayCell", forIndexPath: indexPath) as! reqTodaysCell      
        let cellDataParse: PFObject = self.dataparse.objectAtIndex(indexPath.row) as! PFObject
    
    cell.image.image = reqRead[indexPath.row] ? nil : UIImage(named: "newReq")
    
    return cell
    
    }
    

    它有可能会在加载数组之前尝试填充单元格,但请告诉我这是否适合您。

    【讨论】:

    • 上面的方法和我用的结果一样
    • 好的,这可能是网络问题,我会尝试不同的解决方案
    • 也许可以,如果您有任何想法,请告诉我谢谢 :)
    • 哦,我的上帝,你是一个救生员,谢谢它的工作! ^^
    • 很高兴听到,很高兴我能帮上忙! :]
    【解决方案2】:
       if object["reqRead"] as! Bool == false {
    
                        myCell.isRead.image = nil 
                        myCell.isRead.hidden = false
    
                    } else {
    
                        myCell.isRead.hidden = true
    
                    }
    

    【讨论】:

    • 似乎不起作用...尝试将 hidden 更改为 true 而另一个 false 没有区别...
    • 检查该循环是否执行,然后执行 myCell.isRead.hidden = false - 让 myCell = tableView.dequeueReusableCellWithIdentifier("todayCell", forIndexPath: indexPath) as! reqTodaysCell
    • 我确实在开始时看到了图片,但随后所有图片都消失了,这是错误的。我有三个对象,其中两个是真的,一个是假的,但没有像我想要的那样显示它们......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 2014-01-24
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多