我有一张桌子,上面有细胞图片。然后我想要当用户点击图片的单元格消失并显示一些标签时。但是,每当我尝试从didSelectRowAtIndexPath引用IBoutlet时,它们都返回nil。我在这里做错了什么?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//setting up the table cell to populate the Model Data
var moCell:modelCell = self.tableView.dequeueReusableCellWithIdentifier("moCell") as! modelCell
photo[indexPath.row].getDataInBackgroundWithBlock{
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let modelImage = UIImage(data: imageData!)
moCell.modelPhoto.image = modelImage
}
}
return moCell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var moCell:modelCell = self.tableView.dequeueReusableCellWithIdentifier("moCell") as! modelCell
moCell.top.text = modelsInRange[indexPath.row]
moCell.middle.text = "\(genNumber[indexPath.row]) Generation"
moCell.bottom.text = "\(startYear[indexPath.row]) - \(endYear[indexPath.row])"
}
答案 0 :(得分:1)
您在dequeueReusableCellWithIdentifier(id)
上呼叫didSelectRowAtIndexPath
,因此您无法获得所选的单元格。
我会修改你的cellForRowAtIndexPath
实现并重新加载表数据。在您的单元子类上标记一些标志,以便您可以检查并在cellForRowAtIndexPath
上相应地设置它。或者甚至更好,只需重新加载单个单元格。
我所说的非常快速的样本是:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//setting up the table cell to populate the Model Data
var moCell:modelCell = self.tableView.dequeueReusableCellWithIdentifier("moCell") as! modelCell
if (photo[indexPath.row].isSelected)
{
moCell.top.text = modelsInRange[indexPath.row]
moCell.middle.text = "\(genNumber[indexPath.row]) Generation"
moCell.bottom.text = "\(startYear[indexPath.row]) - \(endYear[indexPath.row])"
} else
{
photo[indexPath.row].getDataInBackgroundWithBlock{
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let modelImage = UIImage(data: imageData!)
moCell.modelPhoto.image = modelImage
}
}
return moCell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
photo[indexPath.row].isSelected = YES
self.tableView.reloadData()
}