2011-08-03 118 views
-1

在我的應用程序中,我使用表視圖來顯示圖像。我在一行中顯示4個圖像。我能夠很好地顯示圖像。但我的問題是,當我滾動表視圖來查看圖像然後滾動發生在一個非常緩慢的速度,也陷入介於兩者之間。可能是什麼原因。我在這裏張貼我的代碼的一部分,請幫助。UITableView滾動問題

- (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; 
} 
+0

看看[這個](http://mobile.tutsplus.com/tutorials/iphone/uitableview-2/) – Sisu

+0

我正在使用表視圖來連續加載多個圖像。但是當我滾動表視圖表視圖崩潰或重疊 –

回答

1

您遇到的最大問題是您正在執行-cellForRowAtIndexPath方法中的所有渲染。這適用於文本和快速迭代的數據,但對於從Web加載的圖像或數據來說,效率非常低。

請記住,對於屏幕上每個可見的單元,此方法都會調用一次。所以如果你有很多處理器密集型的東西在進行,它會使滾動滯後或崩潰。

你應該閱讀Tweetie這個名爲Loren Brichter的文章Fast Scrolling。它解釋瞭如何實現快速滾動出現大量信息的tableview。對於iOS 4來說有些過時了,因爲其中一些方法已經發生了變化,但它足夠讓您朝着正確的方向前進。

+1

無效鏈接:S – filou