//这个函数是 将 indexPath对应的cache值放入字典中,字典的key为indexPath的值,key对应的object为一个数组,数组里面装的是cell内的所有子控件的frame
//这样就将indexPath 对应的cell的所有子控件的frame都给保存了
- (void)setSubviewFrameCache:(CGRect)rect WithIndexPath:(NSIndexPath *)indexPath
{
if (!self.subviewFrameCacheDict) {
self.subviewFrameCacheDict = [NSMutableDictionary new];
}
NSString *cacheKey = [NSString stringWithFormat:@"%ld%ld", (long)indexPath.section, (long)indexPath.row];
NSMutableArray *caches = [self.subviewFrameCacheDict objectForKey:cacheKey];
if (!caches) {
caches = [NSMutableArray new];
[self.subviewFrameCacheDict setValue:caches forKey:cacheKey];
}
//一个一个添加
[caches addObject:[NSValue valueWithCGRect:rect]];
}
本文介绍了一个iOS开发中的实用技巧,通过自定义函数setSubviewFrameCache将UITableView或UICollectionView的cell内子控件的位置信息缓存起来。该函数接收一个CGRect参数和NSIndexPath,将这些值保存到NSMutableDictionary实例中,以便在cell复用时快速恢复布局。
3176

被折叠的 条评论
为什么被折叠?



