셀을 채우는 큰 이미지가있는 테이블 뷰가 있고 행 크기는 이미지 크기를 기반으로 설정됩니다. 불행히도, 다음 셀로 스크롤 할 때 테이블이 잘못 움직입니다.테이블 뷰에 대해 어떻게 캐시합니까?
테이블에로드되기 전에 행 높이와 이미지를 캐시하면 테이블 뷰가 더 부드럽게 스크롤된다는 말을 들었습니다. 내 모든 데이터는 plist에 저장됩니다.
어떻게 캐싱해야합니까? 코드는 어떻게 생겼으며 어디로 이동합니까?
감사합니다. 나는 또한 행 높이를 계산하는 다음 사용하고
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *detailTableViewCellIdentifier = @"Cell";
DetailTableViewCell *cell = (DetailTableViewCell *)
[tableView dequeueReusableCellWithIdentifier:detailTableViewCellIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DetailTableViewCell" owner:self options:nil];
for(id currentObject in nib)
{
cell = (DetailTableViewCell *)currentObject;
}
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *MainImagePath = [Path stringByAppendingPathComponent:([[appDelegate.sectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:@"MainImage"])];
cell.mainImage.image = [UIImage imageWithContentsOfFile:MainImagePath];
return cell;
}
:
여기에 이미지를로드하는 내 코드의
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
AppDelegate *appDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *MainImagePath = [Path stringByAppendingPathComponent:([[appDelegate.sectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:@"MainImage"])];
UIImage *imageForHeight = [UIImage imageWithContentsOfFile:MainImagePath];
imageHeight = CGImageGetHeight(imageForHeight.CGImage);
return imageHeight;
}
편집 : 여기에 아래의 최종 코드입니다.
#define PHOTO_TAG 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Photo";
UIImageView *photo;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIImage *theImage = [UIImage imageNamed:[[appDelegate.sectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:@"MainImage"]];
imageHeight = CGImageGetHeight(theImage.CGImage);
imageWidth = CGImageGetWidth(theImage.CGImage);
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
photo = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageWidth, imageHeight)] autorelease];
photo.tag = PHOTO_TAG;
[cell addSubview:photo];
} else {
photo = (UIImageView *) [cell viewWithTag:PHOTO_TAG];
[photo setFrame:CGRectMake(0, 0, imageWidth, imageHeight)];
}
photo.image = theImage;
return cell;
}
롭을, 응답 해 주셔서 감사합니다. 나는 그 세포에 대한 하나의 견해만을 가지고 있으며 그것은 하나의 이미지이다. 이전 질문에서 이미지 캐싱에 대한 아이디어가 있습니다. http://stackoverflow.com/questions/1352479/tricks-for-improving-iphone-uitableview-scrolling-performance – Jonah
나는 위의 코드를 추가했습니다. 도움. 감사! – Jonah
rpetrich의 답변이 좋습니다. rowHeight 캐싱과 관련하여 가장 쉬운 해결책은 각 행에 대해 계산 된 높이를 저장하는 NSNumbers 배열을 유지하는 것입니다. 모두 0으로 시작하여 시작할 수 있으며, 0이면 계산하여 결과를 배열에 저장할 수 있습니다. 0이 아니면 반환합니다. Instrument에서 몇 가지 프로파일 링을 수행하고 무작위로 변경하기 전에 실제 시간을 보내고 있는지 살펴보십시오. 행 높이 계산은 문제가 될 수 있습니다. 어쩌면 이미지를 너무 자주 스케일링하고있는 것 같습니다 (가능성이 있음). 악기가 도움이 될 것입니다. –