2012-02-06 6 views
4

이것은 하루의 더 좋은 날을 위해 나를 미치게했습니다.셀에 UIImageView 하위 뷰가있을 때 UITableView가 고르지 못한 스크롤을 경험합니다.

나는 UIImageViews가있는 UITableView를 가지고있다. 이 imageviews는 tableview의 cellForRow-function에 로컬로 저장된 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

이 자사의 새로운 셀, ALLOC 이미지 뷰, 형식화하는을 작성하고 추가 할 때마다 원인이 일어나고 .. 내가 깜빡 – vishy

+0

을 if (셀 == nil) 문 안에 whiteBack을 설정하려했지만 슬라이드 이미지에 새 이미지 경로를 추가 할 때 imageview 이미지를 업데이트 할 수 없었습니다. 이 문제를 해결하는 방법에 대한 간단한 예를 들어 주시겠습니까? – PinkFloydRocks

+0

대답에 내 코드를 확인, 업데이트했습니다 .. – vishy

답변

6

먼저 떨어져, 만들어 질 변화의 몇 가지 있습니다 셀이 생성 될 때마다 tableView:cellForRowAtIndexPath:이 히트 (@Vishy가 제안하는 것) 할 때마다가 아니라 UIImageView을 추가해야합니다. 둘째로,로드중인 이미지를 문서 디렉토리에서 캐시해야합니다 ([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

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

셀이 Documents-folder에서 이미지를로드 할 때 스크롤이 여전히 약간 멈 춥니 다.이 작업이 imagedata의 실제로드와 연관 될 수 있습니까? 원본 이미지는 1024x768이지만 내 코드에서 알 수 있듯이 셀 크기와 일치하도록 크기가 조정됩니다. 중지가 원인 일 수 있습니까? – PinkFloydRocks

+0

@PinkFloydRocks 이미지를 캐쉬 할 필요가 있습니다. 크기를 변경 한 후에는 UIViewController 하위 클래스의 NSArray 또는 NSDictionary에 저장해야합니다. –

+0

@EllNeal 슬라이드의 주 이미지와 마찬가지로 축소판 이미지를 생성하여 작동 시켰습니다. 그래서 아마도 이미지의 크기와 그 원인이 된 데이터 였을 것입니다. 그러나 호기심에서 벗어나 이미지를 캐시하는 방법에 대한 작은 예제를 제공 할 수 있습니까? 데이터를 배열에 캐시하고 이미지를 만들 때이 데이터를 사용합니까? – PinkFloydRocks

관련 문제