今天在使用CollectionView寫(xiě)一個(gè)類(lèi)似網(wǎng)頁(yè)信息的demo的時(shí)候,發(fā)現(xiàn)CPU使用達(dá)到了100%, 求助一下大家, 看怎么解決, 代碼如下:
這個(gè)是類(lèi)似網(wǎng)易新聞的樣子,出錯(cuò)也是在這個(gè)里面
#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:
{
//循環(huán)
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的每個(gè)item的width(一個(gè)section只有一個(gè)width)
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(SYStickHeaderWaterFallLayout *)collectionViewLayout
widthForItemInSection:(NSInteger )section; {
return (kDeviceWidth - 30) / 2;
}
// 返回所在indexPath的每個(gè)item的height(每個(gè)item有一個(gè)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里面其實(shí)也沒(méi)什么東西, 這些正常, 就是上面的代碼出了問(wèn)題, 求助:
#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;
}
}
下面是關(guān)于HTTP請(qǐng)求這塊的代碼: 其實(shí)也就是對(duì)上下拉刷新block的一個(gè)回調(diào)的方法:
-(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:@"您還未登入, 請(qǐng)您先登入!" 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);
}
}
}];
}
大家?guī)臀铱纯? 我這是什么問(wèn)題, 謝謝大家了!