【发布时间】:2015-04-15 16:18:05
【问题描述】:
我正在开发表情符号键盘。这是我的方法:
- 我决定使用 UICollectionView。我用代码做所有事情,不打算使用 Xib 文件。
-
我创建了一个 UICollectionViewCell 的子类。这将包含一个显示表情符号的标签。这就是我在它的 initWithFrame 中所做的
- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { if (_label == nil) { _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; _label.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); _label.textAlignment = NSTextAlignmentCenter; [_label setNumberOfLines:1]; self.contentView.layer.cornerRadius = 6.0; [self.contentView addSubview:_label]; } } return self; } -
在 UICollectionView 数据源对象中,我读取了一个包含 NSDictionary 的 plist 文件,其中 NSString 作为键,NSArrays 作为值。在每个 NSArray 中,都可以找到我要展示的表情符号。然后我将字典存储在一个属性中。代码如下:
@property (nonatomic, strong) NSDictionary *emojis; - (NSDictionary *)emojis { if (!_emojis) { NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"EmojisList" ofType:@"plist"]; _emojis = [NSDictionary dictionaryWithContentsOfFile:plistPath]; } return _emojis; } -
在以下方法中,我尝试填充单元格:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSString *cellIdentifier = @"Cell"; EmojiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; cell.label.font = [UIFont systemFontOfSize:self.isiPad ? 47 : 33]; NSArray *dataArray = [self.emojis objectForKey:self.categoryNames[indexPath.section]]; cell.label.text = dataArray[indexPath.row]; return cell; }
我遇到的问题是滚动时内存使用量增加。这会导致在真实设备上崩溃。
请帮助我。我测试了许多不同的方法来解决这个问题,但都没有成功。
这是乐器的截图。我真的不知道这些是关于什么的。
【问题讨论】:
-
您需要运行工具来找出您的代码的哪一部分正在泄漏内存。见:raywenderlich.com/23037/how-to-use-instruments-in-xcode
-
@rjstelling 我已经做过很多次了。这不是我的方法。只有一些与 CFString 或其他东西相关的方法。正如我在下面的评论中所说,当我将字体大小设置为更高的值时,我发现内存使用量更多。快把我逼疯了。
-
@mani 嗨。我看到你帮助了有类似问题的人。你能帮我解决这个问题吗?
标签: ios memory-leaks uicollectionview uilabel uicollectionviewcell