2012-02-06 79 views
4

這讓我在一天中更好的時候發狂。當單元格有UIImageView子視圖時,UITableView體驗波濤洶涌滾動

我有一個UITableView與UIImageViews。這些圖像視圖在tableview的cellForRow函數中加載一個本地保存的PNG文件,並且這種方式可以正常工作,除非tableview停止滾動幾分之一秒,否則當圖像中的一個單元格滾動到可見的位置時可以這麼說。我拖過StackOverflow和谷歌的答案,但我已經來了 - 所以任何幫助將不勝感激。

這裏是我的CellForRow功能代碼:

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


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 



    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    if([currSection isEqualToString:@"composer"]){ 

     MySlide *s = [slidesArray objectAtIndex:indexPath.row]; 

     cell.textLabel.hidden = YES; 
     UIImageView *whiteView = [[UIImageView alloc] initWithFrame:CGRectMake((projectsTable.frame.size.width/2)-150, 4, 204.8, 153.6)]; 

     if([s.slideImage isEqualToString:@""] || s.slideImage == nil){ 
      //no custom image in this cell - go with default background image 

      whiteView.image = [UIImage imageNamed:@"cellback2.png"]; 
      whiteView.backgroundColor = [UIColor whiteColor]; 
     }else{ 
      cell.layer.shouldRasterize = YES; 
      cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 

      NSData *data = [[NSData alloc] initWithContentsOfFile:s.slideImage]; 

      UIImage *im = [[UIImage alloc] initWithData:data]; 


      whiteView.image = im; 

      whiteView.image = [self imageWithImage:whiteView.image CovertToSize:CGSizeMake(204.8,153.6)]; 
      whiteView.backgroundColor = [UIColor whiteColor]; 

     } 


     [cell.contentView addSubview:whiteView]; 

     [whiteView release]; 



     cell.accessoryType = UITableViewCellAccessoryNone; 

    } 


    return cell; 
} 
+2

這是發生導致它的每一個創建一個新的小區,分配一個ImageView的,格式化和添加時間.. – vishy 2012-02-06 11:45:12

+0

我忘了提到我試圖在if(cell == nil)語句中設置whiteBack,但是在將新圖像路徑添加到幻燈片對象時,我無法更新imageview的圖像。你能否給我一個簡單的例子說明你如何解決這個問題? – PinkFloydRocks 2012-02-06 11:51:29

+0

檢查我的代碼在答案,我已經更新.. – vishy 2012-02-06 11:55:35

回答

6

有一對夫婦的變化來進行,先關當細胞生成時應該添加UIImageView s,而不是每次擊中tableView:cellForRowAtIndexPath:(如@Vishy所示)。其次,您應該緩存您從文檔目錄加載的圖像([UIImage imageNamed:]會自動爲捆綁資源執行此操作)。

@interface MyViewController() { 

    NSMutableDictionary *_imageCache; 
} 

@end 


@implementation MyViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // other viewDidLoad stuff... 

    _imageCache = [[NSMutableDictionary alloc] init]; 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 

    // other viewDidUnload stuff... 

    [_imageCache release]; 
    _imageCache = nil; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     UIImageView *whiteView = [[UIImageView alloc] initWithFrame:CGRectMake((projectsTable.frame.size.width/2)-150, 4, 204.8, 153.6)]; 
     whiteView.tag = 111; 
     whiteView.backgroundColor = [UIColor whiteColor]; 

     [cell.contentView addSubview:whiteView]; 

     [whiteView release]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.textLabel.hidden = YES; 
    } 

    UIImageView* iView = (UIImageView*) [cell.contentView viewWithTag:111]; 


    if([currSection isEqualToString:@"composer"]) { 

     MySlide *s = [slidesArray objectAtIndex:indexPath.row]; 

     if([s.slideImage isEqualToString:@""] || s.slideImage == nil) { 

      //no custom image in this cell - go with default background image 
      iView.image = [UIImage imageNamed:@"cellback2.png"]; 
     } 
     else { 

      cell.layer.shouldRasterize = YES; 
      cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 

      // use the image path as the cache key 
      UIImage *theImage = [_imageCache objectForKey:s.slideImage]; 
      if (theImage == nil) { 

       // load the image and save into the cache 
       theImage = [UIImage imageWithContentsOfFile:s.slideImage]; 
       theImage = [self imageWithImage:theImage CovertToSize:CGSizeMake(204.8, 153.6)]; 

       [_imageCache setObject:theImage forKey:s.slideImage]; 
      } 

      iView.image = theImage; 
     } 
    } 
} 

@end 

一般來說,tableView:cellForRowAtIndexPath:是你需要擺脫快速的方法,所以應儘可能避免從磁盤加載圖像。

+0

謝謝 - 這也可以,當處理更多的圖像時,我當前會允許我的用戶添加到幻燈片中。 – PinkFloydRocks 2012-02-06 13:09:58

0

更改代碼按下面...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    UIImageView *whiteView = [[UIImageView alloc] initWithFrame:CGRectMake((projectsTable.frame.size.width/2)-150, 4, 204.8, 153.6)]; 
    whiteView.tag = 111; 
    whiteView.backgroundColor = [UIColor whiteColor]; 

    [cell.contentView addSubview:whiteView]; 

    [whiteView release]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.textLabel.hidden = YES; 

} 

UIImageView* iView = (UIImageView*) [cell.contentView viewWithTag:111]; 


if([currSection isEqualToString:@"composer"]) 
{ 
    MySlide *s = [slidesArray objectAtIndex:indexPath.row]; 

    if([s.slideImage isEqualToString:@""] || s.slideImage == nil) 
    { 
     //no custom image in this cell - go with default background image 

     iView.image = [UIImage imageNamed:@"cellback2.png"]; 
    } 
    else 
    { 
     cell.layer.shouldRasterize = YES; 
     cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 

     iView.image = [UIImage imageWithContentsOfFile:s.slideImage]; 
     iView.image = [self imageWithImage:iView.image CovertToSize:CGSizeMake(204.8,153.6)]; 

    } 
    } 
} 
+0

當一個單元格加載從文檔文件夾中的圖像滾動仍然停留了一點..可以這個停止與實際加載imagedata關聯嗎?原始圖像是1024x768,但是從我的代碼中看到的大小已調整爲與單元格的大小相對應 - 這是否可能導致停止? – PinkFloydRocks 2012-02-06 12:17:53

+0

@PinkFloydRocks你也需要緩存圖片,並將它們存儲在你的'UIViewController'子類的一個'NSArray'或'NSDictionary'中(在你調整它們的大小後)。 – 2012-02-06 12:22:08

+0

@EllNeal我通過生成縮略圖圖像以及幻燈片的主圖像來實現它。所以它可能是圖像的大小和它的數據導致了停止。但出於好奇,你能否提供一個如何緩存圖像的小例子?您是否將數據緩存在數組中,並在創建映像時使用這些數據? – PinkFloydRocks 2012-02-06 12:26:06

相關問題