2015-07-13 106 views
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将允许我这样做。任何帮助,将不胜感激。谢谢!

回答

0

更换

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++ 
} 

随着

for (count, value) in enumerate(item.attendeesImages()) { 

    var workingImage : UIImageView! = nil 
    if count >= cell.attendeesImages.count { 
     workingImage = UIImageView(image : value) 
     cell.addSubview(workingImage) 
     cell.attendeesImages.append(workingImage) 

     //TODO: Layout image based on count 
    } else { 
     workingImage = cell.attendeesImages[count] 
    } 

    workingImage.clipsToBounds = true 
    workingImage.layer.cornerRadius = workingImage.frame.size.width/2 
} 

//Loop through remaining images from cell-reuse and hide them if they exist 
for var index = item.attendeesImages().count; index < cell.attendeesImages.count; ++index { 

    cell.attendeesImages[index].image = nil 
} 
+0

哇,太感谢你了这一点。我明白你的答案是如何工作的,但由于某种原因,我没有得到图像显示..你可能有任何想法,为什么? – user1871869

+0

用以前的方法,我能够得到一个图像来显示,但用这种方法,我似乎无法得到任何显示。 – user1871869

+0

对不起@ user1871869我没有注意到attendeesImages是IB'UIImageViews'的集合。我更新了我的答案,以使用这些现有图像并更新其图像属性。我只是添加了一张支票以确保新图像不超出attendeesImages数组的范围。 – thattyson

相关问题