[英]Swift UIView Memory Usage & Dealing With Many Subviews
I'm making a Word Search app, where I display a large 14x14 grid of letters. 我正在制作一个词搜索应用程序,在其中显示14x14的大型字母网格。 Right now, I display each letter using its own UILabel
as such: 现在,我使用自己的UILabel
显示每个字母,如下所示:
for _ in 0..<14 {
for _ in 0..<14 {
let letterLabel = UILabel()
letterLabel.text = randomLetter()
addSubview(letterLabel)
}
}
However, I'm wondering if I would be better off structuring my code as such: 但是,我想知道是否最好像这样构造我的代码:
let lettersLabel = UILabel()
for _ in 0..<14 {
for _ in 0..<14 {
letterLabel.text += randomLetter() + " "
}
letterLabel.text += "\n"
}
addSubview(letterLabel)
My theory is that this will reduce my app's memory footprint, although I can't find any documentation about exactly how much memory each UILabel
will take up. 我的理论是,尽管我找不到任何有关每个UILabel
会占用多少内存的文档,但是这将减少我的应用程序的内存占用。
But, I'm wondering, how much memory would the latter approach actually save me? 但是,我想知道, 后一种方法实际上可以为我节省多少内存? And which approach is standard/preferred in iOS development? iOS开发中标准/首选哪种方法? Or, is there a third approach I haven't thought of? 或者,有没有我想到的第三种方法?
I feel that the latter approach is better from a performance standpoint, but would greatly complicate the code when it comes to spacing the letters evenly across the screen and determining which letter is tapped. 从性能的角度来看,我认为后一种方法更好,但是在将字母均匀地分布在屏幕上并确定点击哪个字母时,会使代码复杂化。
(Note: This question dramatically simplifies my actual code to highlight the problem area) (注意:这个问题大大简化了我的实际代码,以突出显示问题区域)
Since you want to know which letter is tapped, I would recommend using a collection view. 由于您想知道哪个字母被点击,我建议您使用集合视图。 Here is a sample implementation: 这是一个示例实现:
private let reuseIdentifier = "Cell"
class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
//Here I am generating an array of 14*14 strings since I don't have access to the randomLetter() function
let elements = (0..<(14 * 14)).map { String($0) }
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
self.collectionView!.register(CollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return elements.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
cell.label = UILabel(frame: cell.bounds)
cell.label.text = elements[indexPath.row] //In your case you would use: cell.label.text = randomLetter()
cell.label.font = UIFont(name: "helvetica", size: 10)
cell.label.textAlignment = .center
cell.addSubview(cell.label)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = CGSize(width: self.view.frame.size.width/14, height: self.view.frame.size.width/14)
return size
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! CollectionViewCell
print(String(describing: cell.label.text))
}
}
Where CollectionViewCell
is a custom UICollectionViewCell
defined as follows: 其中CollectionViewCell
是定义如下的自定义UICollectionViewCell
:
class CollectionViewCell: UICollectionViewCell {
var label: UILabel!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.