问题描述:

I want to display my images in a scroll view within my custom tableViewCell. It does display some images but they are wrong. I believe it's due to the fact that they are being reused so where there shouldn't be images, it gets displayed anways.

furthermore, my images get brighter/darker because when i scroll up or down, it gets created again, thus layering more scrollViews on top of each other or just the images.

//global

var masterImageArray = Array<Array<UIImage>>() //holds ALL of my images

//other info for my scrollview to use

let imageWidth: CGFloat = 100

let imageHeight: CGFloat = 200

let yPosition: CGFloat = 0

var xPosition: CGFloat = 0

var scrollViewContentSize: CGFloat = 0

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell: FriendsFeedTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! FriendsFeedTableViewCell

// holds array of UIImages to display in scrollView

let images = masterImageArray[indexPath.row]

//scrollView to display images ([UIImage])

for var i = 0; i < images.count; i++ { //images will vary

let myImage: UIImage = images[i]

let myImageView: UIImageView = UIImageView()

myImageView.image = myImage

myImageView.frame.size.width = imageWidth

myImageView.frame.size.height = imageHeight

myImageView.frame.origin.x = xPosition

cell.imageScrollView.addSubview(myImageView)

xPosition = imageWidth

scrollViewContentSize += imageWidth

cell.imageScrollView.contentSize = CGSize(width: scrollViewContentSize, height: imageHeight)

}

}

My question is, how can I display them properly and how can I stop the "reuse" or from creating others on top of each other. Should Have I put this code inside the tableViewCell file?

----EDIT: 1------

After following @joern 's advice, I went along used 3 UIImageViews which will show the images instead of creating and destroying each UIImageView.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell: FriendsFeedTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! FriendsFeedTableViewCell

let images = masterImageArray[indexPath.row]

var imageViews = [UIImageView]()

imageViews.append(cell.imageView_1)

imageViews.append(cell.imageView_2)

imageViews.append(cell.imageView_3)

for var i = 0; i < images.count; i++ {

imageViews[i].image = images[i]

}

}

网友答案:

There are two main issues here:

  1. You don't increment xPosition

It should be:

xPosition = 0
for var i = 0; i < images.count; i++ { //images will vary
    ...
    xPosition += imageWidth
    ....
}
  1. You don't reset the images from the cell before it is reused.

You have to reset your images before a cell is reused. You do that in your custom cell class. To make that work you should move all the image related code to your cell class. Keep your imageViews in an Array to access them and reset them in prepareForReuse. To keep thing simple I assume in this example that a cell never has more than 3 images:

class CustomTableViewCell: UITableViewCell {

    let imageViews = [UIImageView]()
    ....

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        for _ in 0...2 {
            let imageView = UIImageView()
            // setup and position the image view in your scroll view
            imageViews.append(imageView)
        }
    }

    override func prepareForReuse() {
        for imageView in imageViews {
            imageView.image = nil
        }
    }

    func updateImages(images: [UIImage]) {
        var imageIndex = 0
        for image in images {
            imageViews[imageIndex].image = image
        }
    }
}

Then from in your view controller in cellForRowAtIndexPath you call updateImages on your dequeued cell. prepareForReuse is automatically called by the system, you don't call it yourself.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: FriendsFeedTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! FriendsFeedTableViewCell
    cell.updateImages(masterImageArray[indexPath.row])
    ...
}
相关阅读:
Top