在 scrollView 中显示图像不正确
Displaying images in scrollView are not correct
我想在我的自定义 tableViewCell 中以滚动视图显示我的图像。它确实显示了一些图像,但它们是错误的。我相信这是因为它们被重复使用,所以在不应该有图像的地方,它会显示出来。
此外,我的图像得到 brighter/darker 因为当我向上或向下滚动时,它会再次创建,从而将更多的 scrollViews 叠加在彼此之上或只是图像之上。
//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)
}
}
我的问题是,如何正确显示它们以及如何停止 "reuse" 或在彼此之上创建其他。我是否应该将此代码放入 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
....
}
- 在重新使用单元格之前,您不会重置单元格中的图像。
在重复使用单元格之前,您必须重置图像。您可以在自定义单元格 class 中执行此操作。要完成这项工作,您应该将所有与图像相关的代码移动到您的单元格 class。将您的 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])
...
}
我想在我的自定义 tableViewCell 中以滚动视图显示我的图像。它确实显示了一些图像,但它们是错误的。我相信这是因为它们被重复使用,所以在不应该有图像的地方,它会显示出来。
此外,我的图像得到 brighter/darker 因为当我向上或向下滚动时,它会再次创建,从而将更多的 scrollViews 叠加在彼此之上或只是图像之上。
//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)
}
}
我的问题是,如何正确显示它们以及如何停止 "reuse" 或在彼此之上创建其他。我是否应该将此代码放入 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
....
}
- 在重新使用单元格之前,您不会重置单元格中的图像。
在重复使用单元格之前,您必须重置图像。您可以在自定义单元格 class 中执行此操作。要完成这项工作,您应该将所有与图像相关的代码移动到您的单元格 class。将您的 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])
...
}