【发布时间】:2015-09-07 16:38:38
【问题描述】:
我有一个包含 UIScrollView 的单元格,其中包含一些 UIImageViews 将保存从 Parse 下载的图像。
当它在我的 tableview 中加载前 5 个时效果很好,但在第五个之后,越来越多的 UIImageViews 堆叠在一起。
我相信问题出在我使用的时候:
cell.scrollView.addSubview(myImageView)
这会在 scrollView 中添加并不断添加新的 UIImageView。
我一直在尝试不同的方法来删除 UIImageViews,但我似乎无法正确实现它。
我又回到了原点,但现在我知道是什么导致了问题。
这就是我从 Parse 下载图像的方式:
photoQuery.findObjectsInBackgroundWithBlock {
(objects:[AnyObject]?, error: NSError?) -> Void in
if error == nil {
for object in objects!{
var arrayFile = [PFFile]() // hold both image into temp array
if object.objectForKey("imageOne") != nil {
self.resultsHasImageOneFile.append(object.objectForKey("imageOne") as! PFFile)
arrayFile.append(object.objectForKey("imageOne") as! PFFile)
}
if object.objectForKey("imageTwo") != nil {
self.resultsHasImageTwoFile.append(object.objectForKey("imageTwo") as! PFFile)
arrayFile.append(object.objectForKey("imageTwo") as! PFFile)
}
// will add imageThree
// will add imageFour
// will add imageFive
self.masterImageArray.append(arrayFile)//add temp array into Master Image array
self.resultsTable.reloadData()
}
}
}
这就是我将 UIImageViews 添加到我的滚动视图中的方式:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// configure cell....
var imageArray = masterArray[indexPath.row]
cell.imageScrollView.tag = indexPath.row // do something with this?????
for var i = 0; i < imageArray.count; i++ {
var myImageView: UIImageView = UIImageView()
myImageView.frame.size.width = imageWidth
myImageView.frame.size.height = imageHeight
myImageView.frame.origin.x = xPosition
imageArray[i].getDataInBackgroundWithBlock{
(imageData: NSData?, error: NSError?) -> Void in
myImageView.image = UIImage(data: imageData!)
}
cell.imageScrollView.addSubview(myImageView)
xPosition = imageWidth
scrollViewContentSize += imageWidth
cell.imageScrollView.contentSize = CGSize(width: scrollViewContentSize, height: imageHeight)
cell.imageScrollView.tag = indexPath.row // do something with this?????
}
}
我尝试使用以下内容删除单元格内容中的内容,但它说它找到了 nil,这会崩溃。
func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
var cell = FriendsFeedTableViewCell()
cell.imageScrollView.removeFromSuperview()
}
在我的 FriendsFeedTableViewCell 类中,我只有一个标签和一个名为
的 UIScrollView@IBOutlet weak var imageScrollView: UIScrollView!
如何正确删除已经创建的 UIImageViews?
【问题讨论】:
-
您是否尝试从解析中下载图像然后将其显示到表格视图中
-
它们已经下载。我将 NSData 保存在主阵列中。在 IndexPath,它为他们所做的 NSData 创建 UIImageViews。因为我有透明度(使用 PNG),所以我可以在其他单元格中看到其他 UIImageView。它们是在彼此之上一遍又一遍地创建的。
-
你能贴一张那个tableview的图片吗
-
为什么要在 didEndDisplayingCell 方法中初始化一个新单元格?你能展示你的完整代码以便我们找到问题吗?
-
并显示 FriendsFeedTableViewCell 类
标签: ios swift uitableview uiscrollview uiimageview