我有條件僅顯示幾行圖片。但是當我滾動時,圖像也顯示爲其他隨機行。下面是我的代碼:UITableViewCell圖像在滾動條件下無法正常顯示
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
//var data = objectArray[indexPath.section].sectionObjects[indexPath.row] as? NSDictionary
if let data = objectArray[indexPath.section].sectionObjects[indexPath.row] as? NSDictionary{
if let name = data.objectForKey("NAME") as? String{
cell.textLabel?.text = name
}
if completedTrainings.contains((data.objectForKey("MENU_ID") as? Int)!) {
cell.imageView?.frame = CGRect(x: (cell.bounds.width-110), y: 0, width: 100, height: 100)
cell.imageView?.image = UIImage(named: "CORRECT")
}
}
return cell
}
圖片應該僅在菜單ID在完成培訓存在,但上滾動出現在一些隨機的細胞以及出現。圖像也在左邊,但它應該在右邊。我怎樣才能把它移到正確的位置?
如果要將圖像放在右側,請創建自定義表格視圖單元格。 –
添加此行, cell.imageView?.image = UIImage() –
單元格被重用,您必須通過添加'else'子句將所有UI元素設置爲某個狀態。 – vadian