gpt4 book ai didi

ios - 自定义 UITableViewCell 中的 Swift 可变数量的 subview

转载 作者:可可西里 更新时间:2023-11-01 02:19:33 26 4
gpt4 key购买 nike

我有一个显示自定义单元格的 UITableView。每个单元格都包含一个 ScrollView 、一个 ImageView 和几个文本标签。这一切都已在 Storyboard 中设置。我在 cellForRowAtIndexPath 中设置了文本标签和 ImageView 的各种属性。到目前为止很简单。

现在,我需要在每个单元格的 ScrollView 中添加数量可变的图像。目前,我更新 ScrollView 的内容大小并调用 cell.contentView.addSubview 将这些图像添加到 cellForRowAtIndexPath 中。不幸的是,这似乎会导致内存泄漏。

如果我多次在表格 View 上上下滚动,它会变得非常不稳定和缓慢。我怀疑我添加到单元格的 subview 没有被释放。每次调用“cellForRowAtIndexPath”时,我都会尝试删除单元格的所有 subview ,但这似乎无法解决问题。

我应该在哪里将 subview 添加到单元格的 ScrollView ,以便在每次重用单元格时释放它们?

这是我的代码的一部分:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("newsFeedCell", forIndexPath: indexPath) as! NewsFeedCell
cell.layoutIfNeeded()

// Get the profile picture dimensions
self.profPicWidthHeight = getProfilePictureDimensions(cell)

// Reset all views in the friends subview
for view in cell.friends.subviews {
view.removeFromSuperview()
}

// Get all info to display in cell
cell.eventTitle.text = self.eventNames[indexPath.section]
cell.time.text = self.eventTimes[indexPath.section]
cell.entityPicture.image = self.eventImages[indexPath.section]
cell.entityName.text = self.eventSponserNames[indexPath.section]

// If this user has joined this event, add his/her image to the array
if (self.eventsUserJoined[indexPath.section]) {
self.eventUserImages[indexPath.section].append(self.meImage)
}

// Set the scroll view content size
cell.friends.contentSize = CGSizeMake(CGFloat(self.eventUserImages[indexPath.section].count + 2) * (self.profPicHorizontalOffset + self.profPicWidthHeight), cell.friends.frame.height)

// Add profile images
var i: CGFloat = 1
for profPic in self.eventUserImages[indexPath.section] {
cell.friends.addSubview(layoutProfilePicture(i, picture: profPic!, squareDimension: self.profPicWidthHeight))
i++
}

非常感谢您提供的任何帮助!

最佳答案

您需要使用图像缓存来克服速度缓慢的问题。每次出现在屏幕上时,您的单元格都会重新绘制。如果您不对图像使用 inMemory 缓存,则会导致速度变慢。查看 SDWebImage 并使用它来设置图像。

https://github.com/rs/SDWebImage

使用示例:

let url = NSURL(string: "http://my-images-url.com ")

self.imageView.sd_setImageWithURL(url, completed: block)

关于ios - 自定义 UITableViewCell 中的 Swift 可变数量的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31843277/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com