2016-10-13 63 views
0

我有一個問題,試圖在每個單元格上顯示一個圖像數組,對於tableview。如何在UITableViewCell中設置UIImageView.image屬性

我有一個單獨的TableViewCell Swift文件,我在這個單元格上連接了一個UIImage。出口被命名爲:CellVenueImage

出於某種原因,當我試圖實現我的TableView視圖控制器上的圖像,我得到一個錯誤,指出:Value of type 'TableViewCell' has no member 'CellVenueImage'

下面是其中出現這種情況的行:

cell.CellVenueImage.image = imagename 

使用斯威夫特3.

下面是TableView中的代碼:

import UIKit 

class VenueTableViewController: UITableViewController { 


let VenueImageList = ["1970s.png","1980s.png","1990s.png"] 

override func viewDidLoad() { 
    super.viewDidLoad() 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return VenueImageList.count 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "VenueCell", for: indexPath) as! TableViewCell 

    let imagename = UIImage(named: VenueImageList[indexPath.row]) 
    cell.CellVenueImage.image = imagename 

    return cell 
} 

TableViewCell代碼:

import UIKit 

class VenuesTableViewCell: UITableViewCell { 

@IBOutlet weak var CellVenueImage: UIImageView! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 
+0

代碼TableViewCell請 – zombie

+0

@zombie加,謝謝! – Miles

回答

0

的錯誤是,你沒有使用你的創造TableViewCell子類中,您使用的是常規UITableViewCell

let cell = tableView.dequeueReusableCell(withIdentifier: "VenueCell", for: indexPath) as! TableViewCell 

您需要將它轉換爲您的UITableViewCell子類。

如果它被稱爲「VenuesTableViewCell」,例如:class VenuesTableViewCell: UITableViewCell {,然後將其轉換爲VenuesTableViewCell。

let cell = tableView.dequeueReusableCell(withIdentifier: "VenueCell", for: indexPath) as! VenuesTableViewCell 
+0

這個修好了!非常感謝你。 – Miles

+0

解決方法是:let cell = tableView.dequeueReusableCell(withIdentifier:「VenueCell」,for:indexPath)as! VenuesTableViewCell代替:as! VenueCell – Miles

+0

是的,那正是我試圖告訴你的解釋:)很高興幫助 –

0
let cell = tableView.dequeueReusableCell(withIdentifier: "VenueCell", for: indexPath) as! VenuesTableViewCell 
+0

非常棒,非常感謝。 – Miles