我设置了一个表格,其中包含单元格的图片。然后我希望当用户单击单元格时图片消失并显示一些标签。但是,每当我尝试从 didSelectRowAtIndexPath 引用 IBoutlets 时,它们都会返回 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])"
}
最佳答案
您正在 didSelectRowAtIndexPath
上调用 dequeueReusableCellWithIdentifier(id)
,因此您不会获得选定的单元格。
我会修改您的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()
}
关于ios - 无法从 didSelectRowAtIndexPath 引用 cellForRowAtIndexPath 中的 IBoutlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29830318/