我想在自定义tableViewCell的滚动视图中显示我的图像。它确实显示了一些图像,但它们是错误的。我相信这是因为它们被重复使用,所以在不应该有图像的地方,它会以某种方式显示出来。
此外,我的图像会变得更亮/更暗,因为当我向上或向下滚动时,它会再次被创建,从而将更多的滚动视图叠加在一起,或者只是图像。
//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)
}
}
我的问题是,我如何才能正确地显示它们,如何才能阻止"重用"或在彼此之上创建其他对象。我应该把这个代码放在tableViewCell文件中吗?
----编辑:1-----
在遵循@joern的建议后,我使用了3个UIImageView,它将显示图像,而不是创建和销毁每个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]
}
}
这里有两个主要问题:
- 您不递增
xPosition
应该是:
xPosition = 0
for var i = 0; i < images.count; i++ { //images will vary
...
xPosition += imageWidth
....
}
- 在重用单元格之前,不会重置单元格中的图像
在重用单元格之前,您必须重置图像。您可以在自定义单元格类中执行此操作。为了实现这一点,你应该将所有与图像相关的代码移动到你的单元格类中。将imageViews保存在一个数组中以访问它们,并在prepareForReuse
中重置它们。为了简单起见,我假设在这个例子中,一个细胞的图像永远不会超过3个:
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
}
}
}
然后,在cellForRowAtIndexPath
中的视图控制器中,您在出队单元格上调用updateImages
。prepareForReuse
是由系统自动调用的,您不会自己调用它。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: FriendsFeedTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! FriendsFeedTableViewCell
cell.updateImages(masterImageArray[indexPath.row])
...
}