UITableView滚动问题

时间:2011-08-03 06:12:04

标签: xcode image uitableview scroll

在我的应用程序中,我使用表格视图来显示图像。我连续显示4张图像。我能够很好地显示图像。但我的问题是当我滚动表格视图以查看图像时滚动速度非常慢,而且卡在两者之间。可能是什么原因。我在这里发布我的部分代码,请帮忙。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog([NSString stringWithFormat:@" numberOfRowsInSection :%d",(int)ceil([wordsInSentence count]/4.0)]);
    buttonIndex=1;

    return (int)ceil([wordsInSentence count]/4.0);
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell!=nil) {
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      //return cell;

    }
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    for (int i=0; i < 4; i++) {
      UIImageView *importMediaSaveImage;
      NSString *name1;
      NSString *name2;
      NSString *name3;
      NSString *name4;

      switch (i) {

       case 0:
        importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
        if([wordsInSentence count]>((indexPath.row * 4)+ 0)){

           name1= [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 0)]; 

           importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name1 ofType:@"png" inDirectory:@"Images"]];
           buttonIndex=((indexPath.row * 4)+ 0)+1;
           [self showImage:importMediaSaveImage];
           [imageViewArray addObject:importMediaSaveImage];
           [cell.contentView addSubview:importMediaSaveImage];  
           [imageCollection addObject:importMediaSaveImage];
           NSLog(name1);
        } else {
           importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
           //importMediaSaveImage.image = nil;
           [cell.contentView setHidden:YES];
        }

        break;

      case 1:
        importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
        if([wordsInSentence count]>((indexPath.row * 4)+ 1)){
          name2 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 1)];     
          NSLog(name2);
          importMediaSaveImage.image  = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name2 ofType:@"png" inDirectory:@"Images"]];
          [cell.contentView addSubview:importMediaSaveImage];
          buttonIndex=((indexPath.row * 4)+ 1)+1;
          [self showImage:importMediaSaveImage];
          [imageViewArray addObject:importMediaSaveImage];
          [imageCollection addObject:importMediaSaveImage];
        } else {
          importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
          //importMediaSaveImage.image = nil;
          [cell.contentView addSubview:importMediaSaveImage];
        }
     break;

    case 2:
        importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
        if([wordsInSentence count]>((indexPath.row * 4)+ 2)){
          name3 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 2)]; 
          NSLog(name3);
          importMediaSaveImage.image  = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name3 ofType:@"png" inDirectory:@"Images"]];
          [cell.contentView addSubview:importMediaSaveImage];
          buttonIndex=((indexPath.row * 4)+ 2)+1;
          [self showImage:importMediaSaveImage];
          [imageViewArray addObject:importMediaSaveImage];
          [imageCollection addObject:importMediaSaveImage];
        } else {
          importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
          //importMediaSaveImage.image = nil;
          [cell.contentView addSubview:importMediaSaveImage];
        }
        break;

    case 3:
        importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
        if([wordsInSentence count]>((indexPath.row * 4)+ 3)){
          [cell.contentView addSubview:importMediaSaveImage];   
          name4 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 3)];
          NSLog(name4);
          importMediaSaveImage.image  = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name4 ofType:@"png" inDirectory:@"Images"]];
          buttonIndex=((indexPath.row * 4)+ 3)+1;
          [self showImage:importMediaSaveImage];
          [imageViewArray addObject:importMediaSaveImage];
          [imageCollection addObject:importMediaSaveImage];
        } else {
          importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
          //importMediaSaveImage.image = nil;
          [cell.contentView addSubview:importMediaSaveImage];
        }
        break;

    } //switch end
  } //for loop end

  return cell;
}

1 个答案:

答案 0 :(得分:1)

您遇到的最大问题是您正在-cellForRowAtIndexPath方法中执行所有渲染。这适用于文本和快速迭代的数据,但对于从Web加载的图像或数据,效率非常低。

请记住,对于屏幕上可见的每个单元格,都会调用此方法一次。因此,如果您有大量处理器密集型内容,则会导致滚动延迟或崩溃。

你应该阅读Tweetie成名的Loren Brichter撰写的Fast Scrolling上的这篇文章。它解释了如何使用大量信息快速滚动出tableview。对于iOS 4来说有点过时,因为有些方法已经改变了,但它足够接近让你朝着正确的方向前进。