我正在开发一个表情符号键盘。我的方法如下:
- I decided to use UICollectionView. I do everything in code and don't intend to use Xib files.
I create a subclass of UICollectionViewCell. This is going to contain a single label showing the Emoji. This is what I do inside its 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; }
In UICollectionView dataSource object, I read a plist file containing an NSDictionary with NSString as keys and NSArrays as values. Inside each NSArray, one can find the emojis I'm going to show. I then store the dictionary in a property. Here is the code:
@property (nonatomic, strong) NSDictionary *emojis; - (NSDictionary *)emojis { if (!_emojis) { NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"EmojisList" ofType:@"plist"]; _emojis = [NSDictionary dictionaryWithContentsOfFile:plistPath]; } return _emojis; }
In the following method, I try to populate the cells:
- (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; }
我的问题是,当我滚动时,内存使用量会增加。这导致真实设备上崩溃。
请帮助我。我尝试了许多不同的解决方法,但都没有成功。
这是仪器截图。我真的不知道这些是关于什么的。