今天在使用CollectionView写一个类似网页信息的demo的时候,发现CPU使用达到了100%, 求助一下大家, 看怎么解决, 代码如下:
这个是类似网易新闻的样子,出错也是在这个里面
#pragma mark iCarouselDelegate, iCarouselDataSource
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return 3;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(nullable UIView *)view {`
SYStickHeaderWaterFallLayout *cvLayout = [[SYStickHeaderWaterFallLayout alloc] init];
cvLayout.delegate = self;
cvLayout.stickyHeader = YES;
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height) collectionViewLayout:cvLayout];
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.backgroundColor = [UIColor whiteColor];
collectionView.tag = index + 100;
switch (collectionView.tag) {
case 100:
collectionView.backgroundColor = [UIColor redColor];
break;
case 101:
collectionView.backgroundColor = [UIColor greenColor];
break;
case 102:
collectionView.backgroundColor = [UIColor blackColor];
break;
default:
break;
}`
// 下拉重新加载
collectionView.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
showPage = 1;
switch (collectionView.tag) {
case 100:
[self.xgLiveFocus removeAllObjects];
break;
case 101:
[self.xgLiveNears removeAllObjects];
break;
case 102:
[self.xgLiveHots removeAllObjects];
break;
default:
break;
}
[self requestHomePageList:showPage refreshType:@"header" liveType:collectionView.tag headerCallBack:^(BOOL success, id responseObject, AppError *error) {
// dispatch_async(dispatch_get_main_queue(), ^{
if (success) {
switch (collectionView.tag) {
case 100:
[self.xgLiveFocus addObjectsFromArray:responseObject];
break;
case 101:
[self.xgLiveNears addObjectsFromArray:responseObject];
break;
case 102:
[self.xgLiveHots addObjectsFromArray:responseObject];
default:
break;
}
[collectionView reloadData];
}
[collectionView.mj_header endRefreshing];
// });
} footerCallBlock:^(BOOL success, id responseObject, AppError *error) {
}];
}];
// 上拉加载更多
collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
showPage += 1;
[self requestHomePageList:showPage refreshType:@"footer" liveType:collectionView.tag headerCallBack:^(BOOL success, id responseObject, AppError *error) {
} footerCallBlock:^(BOOL success, id responseObject, AppError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (success) {
switch (collectionView.tag) {
case 100:
[self.xgLiveFocus addObjectsFromArray:responseObject];
break;
case 101:
[self.xgLiveNears addObjectsFromArray:responseObject];
break;
case 102:
[self.xgLiveHots addObjectsFromArray:responseObject];
default:
break;
}
[collectionView reloadData];
}
[collectionView.mj_footer endRefreshing];
});
}];
}];
[collectionView registerNib:[UINib nibWithNibName:xgHomeCollectionCell bundle:nil] forCellWithReuseIdentifier:xgHomeCollectionCell];
[collectionView reloadData];
[collectionView.mj_header beginRefreshing];
return collectionView;
}
- (void)carouselDidEndDecelerating:(iCarousel *)carousel
{
// [self updateScrollViewButtonForIndex:carousel.currentItemIndex];
[self setNavScrollViewsForIndex:carousel.currentItemIndex];
}
- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
//implement 'flip3D' style carousel
transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * self.icarouselView.itemWidth);
}
- (CGFloat)carousel:(__unused iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
//customize carousel display
switch (option)
{
case iCarouselOptionWrap:
{
//循环
return NO;
}
case iCarouselOptionSpacing:
{
//add a bit of spacing between the item views
return value ;//* 1.05f;
}
case iCarouselOptionFadeMax:
{
if (self.icarouselView.type == iCarouselTypeCustom)
{
//set opacity based on distance from camera
return 0.0f;
}
return value;
}
case iCarouselOptionShowBackfaces:
case iCarouselOptionRadius:
case iCarouselOptionAngle:
case iCarouselOptionArc:
case iCarouselOptionTilt:
case iCarouselOptionCount:
case iCarouselOptionFadeMin:
case iCarouselOptionFadeMinAlpha:
case iCarouselOptionFadeRange:
case iCarouselOptionOffsetMultiplier:
case iCarouselOptionVisibleItems:
{
return value;
}
}
}
#pragma mark SYStickHeaderWaterFallDelegate
// 返回所在section的每个item的width(一个section只有一个width)
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(SYStickHeaderWaterFallLayout *)collectionViewLayout
widthForItemInSection:(NSInteger )section; {
return (kDeviceWidth - 30) / 2;
}
// 返回所在indexPath的每个item的height(每个item有一个height,要不然怎么是瀑布流)
- (CGFloat)collectionView:(nonnull UICollectionView *)collectionView
layout:(nonnull SYStickHeaderWaterFallLayout *)collectionViewLayout
heightForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
LivesModel *model;
switch (collectionView.tag) {
case 100:
model = [self.xgLiveFocus objectAtIndex:indexPath.item];
break;
case 101:
model = [self.xgLiveNears objectAtIndex:indexPath.item];
break;
case 102:
model = [self.xgLiveHots objectAtIndex:indexPath.item];
break;
default:
break;
}
return [self calculateImageHeigthFor:model.liveLogoWidth height:model.liveLogoHeight];;
}
- (CGFloat)calculateImageHeigthFor:(CGFloat)width height:(CGFloat)height {
return ((kDeviceWidth - 30) /2 ) * height / width;
}
CollectionView里面其实也没什么东西, 这些正常, 就是上面的代码出了问题, 求助:
#pragma mark CollectionView Delegate,DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
// [collectionView.collectionViewLayout invalidateLayout];
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
NSInteger itemCount;
switch (collectionView.tag) {
case 100:
itemCount = self.xgLiveFocus.count;
break;
case 101:
itemCount = self.xgLiveNears.count;
break;
case 102:
itemCount = self.xgLiveHots.count;
default:
break;
}
return itemCount;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HomeCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:xgHomeCollectionCell forIndexPath:indexPath];
switch (collectionView.tag) {
case 100:
cell.model = self.xgLiveFocus[indexPath.item];
break;
case 101:
cell.model = self.xgLiveNears[indexPath.item];
break;
case 102:
cell.model = self.xgLiveHots[indexPath.item];
break;
default:
break;
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
index = indexPath.row;
self.isAnchor = NO;
switch (collectionView.tag) {
case 100:
[self performSegueWithIdentifier:@"LivingIdentifier" sender:self.xgLiveFocus[indexPath.item]];
break;
case 101:
[self performSegueWithIdentifier:@"LivingIdentifier" sender:self.xgLiveNears[indexPath.item]];
break;
case 102:
[self performSegueWithIdentifier:@"LivingIdentifier" sender:self.xgLiveHots[indexPath.item]];
break;
default:
break;
}
}
下面是关于HTTP请求这块的代码: 其实也就是对上下拉刷新block的一个回调的方法:
-(void)requestHomePageList:(NSInteger )page refreshType:(NSString *)refreshtype liveType:(XGLivingType )livingType headerCallBack:(HTTPHeaderBlock)headerBlock footerCallBlock:(HTTPFooterBlock)footerBlock {
NSString *memberID = [Login getMember_ID];
if (memberID == nil) {
[NSObject showHudTipString:@"您还未登入, 请您先登入!" delay:2.0];
return;
}
NSDictionary *dict = @{@"pageIndex":[NSNumber numberWithInteger:page],
@"longitude":@(113.232970),
@"latitude":@(23.398850),
@"isLive":[NSNumber numberWithBool:true],
@"memberId":[Login getMember_ID]
};
[XGAPI getXGLivingList:dict livingType:livingType CallBack:^(BOOL success, id responseObject, AppError *error) {
if (success) {
NSLog(@"responseObject :%@",responseObject);
if (responseObject) {
if ([refreshtype isEqualToString:@"header"]) {
if (headerBlock) {
headerBlock(success, responseObject, error);
}
} else {
footerBlock(success, responseObject, error);
}
}
} else {
if ([refreshtype isEqualToString:@"header"]) {
headerBlock(success, responseObject, error);
} else {
footerBlock(success, responseObject, error);
}
}
}];
}
大家帮我看看, 我这是什么问题, 谢谢大家了!