Ios Uitableview带懒散加载的滚动滞后(苹果源代码)

Ios Uitableview带懒散加载的滚动滞后(苹果源代码),ios,objective-c,uitableview,Ios,Objective C,Uitableview,我想弄明白为什么我的程序在模拟器上运行得很好,但在手机上却不行 当我滚动我的表格时,它会变慢 我尝试了一切,甚至删除了我的图片加载,但仍然发生滚动延迟 Im使用apple lazy加载源代码: 这是我的代码: #define kCustomRowCount 7 @interface SubCategoryViewController () <UIScrollViewDelegate> @property (nonatomic, strong) NSMutableDict

我想弄明白为什么我的程序在模拟器上运行得很好,但在手机上却不行

当我滚动我的表格时,它会变慢

我尝试了一切,甚至删除了我的图片加载,但仍然发生滚动延迟

Im使用apple lazy加载源代码:

这是我的代码:

#define kCustomRowCount     7


@interface SubCategoryViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableDictionary *imageDownloadsInProgress;
@property (nonatomic, strong) NSMutableDictionary *imageRestitleDownloadsInProgress;
@property (nonatomic, assign) int intindex;
@end


@implementation SubCategoryViewController

#pragma mark


- (void)viewDidLoad
{

    [super viewDidLoad];
    self.title = self.catname;
    self.imageDownloadsInProgress = [NSMutableDictionary dictionary];
    self.imageRestitleDownloadsInProgress = [NSMutableDictionary dictionary];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

    NSArray *allDownloads = [self.imageDownloadsInProgress allValues];
    [allDownloads makeObjectsPerformSelector:@selector(cancelDownload)];

    NSArray *allDownloads2 = [self.imageRestitleDownloadsInProgress allValues];
    [allDownloads2 makeObjectsPerformSelector:@selector(cancelDownload)];

    [self.imageDownloadsInProgress removeAllObjects];
    [self.imageRestitleDownloadsInProgress removeAllObjects];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger count = [self.entries count];

    // if there's no data yet, return enough rows to fill the screen
    if (count == 0)
    {
        return kCustomRowCount;
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *PlaceholderCellIdentifier = @"PlaceholderCell2";


    NSUInteger nodeCount = [self.entries count];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
    cell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    UILabel *newsbody = (UILabel *)[cell.contentView viewWithTag:6];
    UIImageView *uimg = (UIImageView *)[cell.contentView viewWithTag:20];
    UIImageView *uimgresttile = (UIImageView *)[cell.contentView viewWithTag:23];


    if (nodeCount == 0 && indexPath.row == 0)
    {


        if (cell == nil) {

            uimg.hidden = true;
            uimgresttile.hidden = true;
            newsbody.hidden = true;


        }
        else
        {
            uimg.hidden = true;
            uimgresttile.hidden = true;
            newsbody.hidden = true;


        }

        return cell;
    }
    if (nodeCount == 0 && indexPath.row > 0)
    {
        cell.hidden = true;
        return cell;
    }



    if (nodeCount > 0)
    {

        uimg.hidden = false;
        uimgresttile.hidden = false;
        newsbody.hidden = true;

        NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
        [newsbody setText:appRecord.Body];

        if (!appRecord.appIcon)
        {
             if (self.tableView.dragging == NO && self.tableView.decelerating == NO)

             {
                 [self startIconDownload:appRecord forIndexPath:indexPath];

             }


             uimg.image = [UIImage imageNamed:@"Placeholder.png"];

         }
         else
         {
             uimg.image = appRecord.appIcon;

         }
         if (!appRecord.appIconrestitle)
         {
             if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
             {
                  [self startRestitleIconDownload:appRecord forIndexPath:indexPath];

             }


              uimgresttile.image = [UIImage imageNamed:@"Placeholder.png"];
         }
         else
         {

              uimgresttile.image = appRecord.appIconrestitle;
         }
    }
     if ((unsigned long)indexPath.row == [self.entries count] - 1){
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
                        ^{
                            NewsFetchParseOperation *p = [[NewsFetchParseOperation alloc]init];
                            NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];


                            p.cat = appRecord.Category;
                            self.intindex = self.intindex + 1;
                            p.index = [NSString stringWithFormat:@"%d", (int)self.intindex];
                            p.lastid = appRecord.ids;
                            [p main];
                            dispatch_async(dispatch_get_main_queue(), ^(void)
                                           {

                                                SubCategoryViewController *svc =[self.storyboard instantiateViewControllerWithIdentifier:@"SubCategory"];
                                               NSArray *temp =[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
                                                self.entries = temp;
                                               [self.tableView reloadData];

                                           });
                        });

     }
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *PlaceholderCellIdentifier = @"PlaceholderCell2";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];

    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));

    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    height += 1;

    return height;

}

- (void)startIconDownload:(NewsFetchAppRecord *)appRecord forIndexPath:(NSIndexPath *)indexPath
{
    NewsFetchIconDownloader *iconDownloader = [self.imageDownloadsInProgress objectForKey:indexPath];

    if (iconDownloader == nil)
   {
        iconDownloader = [[NewsFetchIconDownloader alloc] init];
        iconDownloader.newsfetchappRecord = appRecord;
        [iconDownloader setCompletionHandler:^{

            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            UIImageView *uimg = (UIImageView *)[cell.contentView viewWithTag:20];
            uimg.image = appRecord.appIcon;

            [self.imageDownloadsInProgress removeObjectForKey:indexPath];

        }];
        [self.imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];
       [iconDownloader startDownload];
    }

}
- (void)startRestitleIconDownload:(NewsFetchAppRecord *)appRecord forIndexPath:(NSIndexPath *)indexPath
{

    NewsFetchRestitleIconDownloader *ResttitleiconDownloader = [self.imageRestitleDownloadsInProgress objectForKey:indexPath];

    if (ResttitleiconDownloader == nil)
    {
        ResttitleiconDownloader = [[NewsFetchRestitleIconDownloader alloc] init];
        ResttitleiconDownloader.newsfetchappRecord = appRecord;
        [ResttitleiconDownloader setCompletionHandler:^{

            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            UIImageView *uimgresttile = (UIImageView *)[cell.contentView viewWithTag:23];

            uimgresttile.image = appRecord.appIconrestitle;


            [self.imageRestitleDownloadsInProgress removeObjectForKey:indexPath];

        }];
        [self.imageRestitleDownloadsInProgress setObject:ResttitleiconDownloader forKey:indexPath];
        [ResttitleiconDownloader startDownload];
    }
}

- (void)loadImagesForOnscreenRows
{
    if ([self.entries count] > 0)
    {
        NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
        for (NSIndexPath *indexPath in visiblePaths)
        {
            NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];

            if (!appRecord.appIcon)
            {
                [self startIconDownload:appRecord forIndexPath:indexPath];
            }

        }
    }
}



- (void)loadRestitleImagesForOnscreenRows
{
    if ([self.entries count] > 0)
   {
        NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
        for (NSIndexPath *indexPath in visiblePaths)
        {
           NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];

           if (!appRecord.appIconrestitle)
            {
                [self startRestitleIconDownload:appRecord forIndexPath:indexPath];
            }
       }
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate)
    {
        [self loadImagesForOnscreenRows];
      [self loadRestitleImagesForOnscreenRows];
     }
  }

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self loadImagesForOnscreenRows];
   [self loadRestitleImagesForOnscreenRows];
}

@end
#定义kCustomRowCount 7
@接口子类别ViewController()
@属性(非原子,强)NSMutableDictionary*imageDownloadsInProgress;
@属性(非原子,强)NSMutableDictionary*imageRestitleDownloadsInProgress;
@属性(非原子,赋值)intindex;
@结束
@实现子类别ViewController
#布拉格标记
-(无效)viewDidLoad
{
[超级视图下载];
self.title=self.catname;
self.imageDownloadsInProgress=[NSMutableDictionary];
self.imageRestitleDownloadsInProgress=[NSMutableDictionary];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
NSArray*allDownloads=[self.imageDownloadsInProgress allValues];
[所有下载makeObjectsPerformSelector:@selector(cancelDownload)];
NSArray*allDownloads2=[self.imageRestitleDownloadsInProgress allValues];
[allDownloads2 makeObjectsPerformSelector:@selector(cancelDownload)];
[self.imagedownloads正在删除所有对象];
[self.imageRestitleDownloadsInProgress removeAllObjects];
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
返回(interfaceOrientation!=UIInterfaceOrientation肖像向上向下);
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
NSU整数计数=[self.entries计数];
//如果还没有数据,返回足够的行以填充屏幕
如果(计数=0)
{
返回kCustomRowCount;
}
返回计数;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*占位符CellIdentifier=@“占位符Cell2”;
NSUTEGER nodeCount=[self.entries count];
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:Placeholder CellIdentifier];
cell.contentView.translatesAutoresizingMaskIntoConstraints=否;
UILabel*新闻正文=(UILabel*)[cell.contentView视图带标记:6];
UIImageView*uimg=(UIImageView*)[cell.contentView视图带标记:20];
UIImageView*uimgresttile=(UIImageView*)[cell.contentView视图带标记:23];
if(nodeCount==0&&indexath.row==0)
{
如果(单元格==nil){
uimg.hidden=true;
uimgresttile.hidden=true;
newsbody.hidden=true;
}
其他的
{
uimg.hidden=true;
uimgresttile.hidden=true;
newsbody.hidden=true;
}
返回单元;
}
if(nodeCount==0&&indexath.row>0)
{
cell.hidden=true;
返回单元;
}
如果(节点计数>0)
{
uimg.hidden=false;
uimgresttile.hidden=false;
newsbody.hidden=true;
NewsFetchAppRecord*appRecord=[self.entries objectAtIndex:indexPath.row];
[newsbody setText:appRecord.Body];
如果(!appRecord.appIcon)
{
if(self.tableView.dragging==NO&&self.tableView.decreating==NO)
{
[self-startIconDownload:appRecord-forindexath:indexPath];
}
uimg.image=[UIImage imagename:@“Placeholder.png”];
}
其他的
{
uimg.image=appRecord.appIcon;
}
如果(!appRecord.appIconrestitle)
{
if(self.tableView.dragging==NO&&self.tableView.decreating==NO)
{
[self startRestitleIconDownload:appRecord forIndexPath:indexPath];
}
uimgresttile.image=[UIImage ImageName:@“Placeholder.png”];
}
其他的
{
uimgrestile.image=appRecord.appIconrestitle;
}
}
if((无符号长)indexath.row==[self.entries count]-1){
调度异步(调度获取全局队列(调度队列优先级高,0),
^{
NewsFetchParseOperation*p=[[NewsFetchParseOperation alloc]init];
NewsFetchAppRecord*appRecord=[self.entries objectAtIndex:indexPath.row];
p、 cat=批准类别;
self.intindex=self.intindex+1;
p、 索引=[NSString stringWithFormat:@“%d”,(int)self.intindex];
p、 lastid=appRecord.ids;
[p main];
dispatch\u async(dispatch\u get\u main\u queue(),^(void)
{
子类别ViewController*svc=[self.storyboard instanceeviewcontrollerwhiteIdentifier:@“子类别”];
NSArray*temp=[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
self.entries=temp;
[self.tableView重载数据];
});
});
}
返回单元;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径
{
静态NSString*占位符CellIdentifier=@“占位符Cell2”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:Placeholder CellIdentifier];
[单元格设置需要SupdateConstraints];
[单元格更新应变如果需要];
cell.bounds=CGRectMake(0.0f,0.0f,CGRectGetWidth(tableView.bounds),CGRectGetHeight(cell.bounds));
[单元格设置需要删除];
[需要细胞布局];
CGFloat height=[cell.contentView系统布局大小设置大小:UILayoutFittingCompressedSize]。高度;
[uimg setImageWithURL:"http://blahbahblah.com" placeholderImage:someUIIMage]