0
我遇到了一个问题,我不知道如何解决。我有一个Cell
对象,我创建了一些IBOutlets
,我想我UITableView
里显示我的屏幕,像这样:在imageView数组中设置图像?
class EventCell: UITableViewCell, CellDelegate{
@IBOutlet var eventName: UILabel!
@IBOutlet var eventLocation: UILabel!
@IBOutlet var attendeesImages: [UIImageView]!
}
我也有另外一个功能,我尝试设置等,以使单元格的单元格内容:
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()
//Attempt to set the cell.attendeesImage values
for value in item.attendeesImages() {
cell.attendeesImages[count].image = value
cell.attendeesImages[count].clipsToBounds = true
cell.attendeesImages[count].layer.cornerRadius = cell.attendeesImage[count].frame.size.width/2
count++
}
}
的问题是,当我尝试设置的cell.attendeesImages
的价值观,我碰到一个问题,说fatal error: Array index out of range
。问题是因为我正在访问cell.attendeesImages
中不存在的索引。在分配内容之前,有没有办法分配或设置cell.attendeesImages
的大小?或者在cell.attendeesImages
中有更好的方法来分配图像吗?我也尝试将cell.attendeesImages
设置为UIImage
和设置cell.attendeesImages = item.attendeesImages
的数组,但如果它是UIImage
,我似乎无法将图像显示在屏幕上,而UIImageView
将允许我这样做。任何帮助,将不胜感激。谢谢!
哇,太感谢你了这一点。我明白你的答案是如何工作的,但由于某种原因,我没有得到图像显示..你可能有任何想法,为什么? – user1871869
用以前的方法,我能够得到一个图像来显示,但用这种方法,我似乎无法得到任何显示。 – user1871869
对不起@ user1871869我没有注意到attendeesImages是IB'UIImageViews'的集合。我更新了我的答案,以使用这些现有图像并更新其图像属性。我只是添加了一张支票以确保新图像不超出attendeesImages数组的范围。 – thattyson