3

我是IOS开发的新手。我正在使用自定义 GridView 在我的项目中加载内容,并且在重新加载内容时它对模拟器工作正常。但在 iPad 中,重新加载时内容会随机播放。

  - (void)reloadData {
        if (_gridViewDelegate && self.superview != nil) {
            CGFloat maxX = 0.f;
            CGFloat maxY = 0.f;
            _scrollView.pagingEnabled = YES;
            self.gridRects = [self.gridViewDelegate rectsForCellsInGridView:self];
            [self.gridCells.allValues makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [self.reuseableCells addObjectsFromArray:self.gridCells.allValues];
            [self.gridCells removeAllObjects];

            for (NSValue *rectValue in self.gridRects) {
                CGRect rect = [rectValue CGRectValue];
                maxX = MAX(maxX, rect.origin.x + rect.size.width);
                maxY = MAX(maxY, rect.origin.y + rect.size.height);


            }
            CGFloat pageWidth = self.scrollView.frame.size.width;
            self.maximumContentWidth=maxX;
            maxX = MAX(MIN(maxX, self.maximumContentWidth), self.contentSize.width);
            maxY = MAX(MIN(maxX, self.maximumContentHeight), self.contentSize.height);
            maxX=pageCount*pageWidth;          
            self.scrollView.contentSize = CGSizeMake(maxX, maxY);
            [self loadCellsInRect:self.visibleRect];
            [self updateStickyViewsPosition];
            [pageNumberLable removeFromSuperview];          
            currentPage = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

            if(pageWidth == 1024)
            {

                pageNumberLable=[[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.contentOffset.x+990, 690, 20, 20)];
                pageNumberLable.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
            }
            else
            {
                 pageNumberLable=[[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.contentOffset.x+740, 943, 20, 20)];
                pageNumberLable.backgroundColor=[UIColor redColor];
                pageNumberLable.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

            }

            pageNumberLable.text=[NSString stringWithFormat:@"%d",currentPage+1];
            pageNumberLable.textColor=[UIColor blackColor];
            pageNumberLable.font = [UIFont boldSystemFontOfSize:16];
            pageNumberLable.backgroundColor=[UIColor clearColor];
            [self.scrollView addSubview:pageNumberLable]; 
            pageIndex = [NSUserDefaults standardUserDefaults];            
            [pageIndex setInteger:currentPage+1 forKey:@"integerKey"];         
        }    
    }

        -(void)reloadGrid
        {
            if(rotate == newIntValue)
            {
                [spinningLibrary stopAnimating];
                [update removeFromSuperview];
                settingLabel.hidden=YES;
                spinningLibrary.hidden=YES;
            }

            if(rotate<newIntValue)
            {
                rotate++;
                [self.gridView scrollRectToVisible:CGRectMake(768*rotate, 0, self.gridView.frame.size.width, self.gridView.frame.size.height) animated:YES];
                [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(reloadGrid123) userInfo:nil repeats:NO];

            }
            [NSThread sleepForTimeInterval:0.2];
            self.gridView.scrollView.hidden=NO;
            [settingsPopoverController dismissPopoverAnimated:YES];

        }
4

1 回答 1

2

您正在比较 iPad 的 RAM 和您机器的 RAM。但这并不公平。

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(reloadGrid123) userInfo:nil repeats:NO];

尝试通过设置来调整间隔(> 0.1)scheduledTimerWithTimeInterval

这是阻塞主线程的不好方法。

使用以下块方法来“在某个时间后做某事”。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

//  Do something taking a long time in background.

    [NSThread sleepForTimeInterval:0.2];

    //  Background thread has finished it's work.
    //  Call another block on main thread to do UI stuff like updating etc..
    dispatch_async(dispatch_get_main_queue(), ^{
        //  Here you are in the main thread again
        //  You can do whatever you want

    });
});
于 2013-09-23T05:40:25.830 回答