Swift:Bool如果为真,则不';t显示图像
我是初学者,请耐心解释,谢谢 所以,基本上,我在parse中有一个bool列,如果它为false,我想显示一个图像,如果它为true,则不显示任何内容 这是我的密码:Swift:Bool如果为真,则不';t显示图像,swift,parse-platform,tableviewcell,Swift,Parse Platform,Tableviewcell,我是初学者,请耐心解释,谢谢 所以,基本上,我在parse中有一个bool列,如果它为false,我想显示一个图像,如果它为true,则不显示任何内容 这是我的密码: override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let myCell = tableView.dequeueReusableCellWi
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")
}
}
}
})
如果我没有正确解释,请让我知道,我会尽力再次解释。这听起来像是三元运算符的理想用例。根据下面的示例,您可以使用?:Bool后面的语法,如果Bool为true,则返回第一个大小写,如果为false,则返回第二个大小写
if object["reqRead"] as! Bool == false {
myCell.isRead.image = nil
myCell.isRead.hidden = false
} else {
myCell.isRead.hidden = true
}
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")
}
}
})
更新
上述操作可能不起作用,因为在加载单元格之前,解析调用可能无法完成
创建全局变量(在任何函数之外):
在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()
}
})
然后在你的牢房里:
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
}
它可能会在加载阵列之前尝试填充单元格,但请告诉我这是否适用于您。似乎不起作用。。。尝试将hidden更改为true,另一个更改为false没有区别…检查该循环是否正在执行,并在-let myCell=tableView.dequeueReusableCellWithIdentifier(“todayCell”,forIndexPath:indexPath)之后执行myCell.isRead.hidden=false!reqTodaysCelli确实看到了开头的图片,但随后所有内容都消失了,这是错误的。我有三个对象,其中两个是真的,一个是假的,但没有像我想要的那样显示它们…上面的方法与我使用的方法有相同的结果。这可能是网络问题,我会尝试不同的解决方案,也许,如果有任何想法击中你,请告诉我谢谢:)哦,我的上帝,你是一个救生员,谢谢它奏效了^^听到这个消息太好了,很高兴我能帮忙!:]
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
}