2015-07-13 117 views
0

我有一個UIImageViews的數組,我想要在屏幕上顯示。但是,我似乎無法做到這一點。我宣佈我的UIImageViews陣列像這樣:UIImageView Array未顯示內容?

class EventCell: UITableViewCell, CellDelegate{ 
    @IBOutlet var eventName: UILabel! 
    @IBOutlet var eventLocation: UILabel! 
    @IBOutlet var attendeesImages: [UIImageView]! 
} 

而且我有一個顯示的EventCell內容像這樣的功能:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    //Dequeue a "reusable" cell 
    let cell = tableView.dequeueReusableCellWithIdentifier(eventCellIdentifier) as! EventCell 
    setCellContents(cell, indexPath: indexPath) 
    return cell 
} 

//Set contents of Event Cell.. self.events is a global array 
//which have information that EventCell objects need to display 

func setCellContents(cell:EventCell, indexPath: NSIndexPath!){ 
    let item = self.events[indexPath.section] 
    var count = 0 

    cell.eventName.text = item.eventName() 
    cell.eventLocation.text = item.eventLocation() 

    cell.attendeesImage.removeAll(keepCapacity: false)//Remove old images before adding new 
    for value in item.attendeesImage { 
     let newImageView = UIImageView(image : value) 
     newImageView.clipsToBounds = true 
     newImageView.layer.cornerRadius = newImageView.frame.size.width/2 
     cell.attendeesImage.append(newImageView) 
     println("Count inside: \(cell.attendeesImage.count)") 
    } 
} 

我打印出來的cell.attendeesImagecount,以確保沒有多餘的圖片正在被添加,它似乎證明是正確的。但是,我不知道爲什麼我的圖像不顯示。我確定在EventCell中將cell.attendeesImage作爲IBCollection掛在我的Storyboard中(因爲它是一個Array)。任何想法爲什麼這不顯示?謝謝!

+0

如何在你的表格視圖單元格中佈置圖像? – ozgur

+0

@ozgur那一步我還沒有完成,我也不完全確定如何做這部分。在我解決這個問題之後,我正在研究它。有什麼建議麼? – user1871869

+0

如果你還沒有實現那部分,那麼我不太明白你的問題是什麼。代碼負責在你的單元格中顯示圖像在哪裏?您是否檢查過像cell.contentView.addSubview(newImageView)' – ozgur

回答

0

您正在使用錯誤的收集插座「attendeesImages」。

加載視圖時,將使用在界面構建器中創建的圖像視圖創建和初始化陣列attendeesImages。但是,如果你刪除所有的參考文獻,你就失去了網點和真實觀點之間的聯繫。您創建的新圖像視圖不是單元格中的視圖。他們仍然是單元格的子視圖,但現在你還沒有iboutlet引用它們。

解決方案:不要從數組中刪除值。只需在每個圖像視圖中修改圖像即可。

+0

每個單元格的圖像總數可能不同,因此刪除'prepareForReuse'中的所有內容並在委託方法中重新填充它們對我來說更有意義。 – ozgur

+0

啊好的。所以我拿出'cell.attendeesImage'這一行。removeAll(keepCapacity:false)''但這似乎讓事情變得更糟。實際上,當我打印出'cell.attendeesImage.count'時,計數會不斷增加,我認爲這會影響實際顯示的內容。對此有任何修正? – user1871869

+0

@ozgur你介意給我一個你上面提到的事情的輪廓? – user1871869