2016-10-13 60 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