2012-02-15 9 views
1

이 내 문제입니다. 스크롤 할 때 UITableViewController가 지연됩니다. 그것에는 대략 60의 주문 세포가있다. 그러나 시뮬레이터에서는 어떤 지체없이 작동합니다. ,스크롤 할 때 UITableView의 뒤가

#import "CustomCell.h" 

@implementation CustomCell 

@synthesize nameLabel,costLabel,giftPicture; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 


     nameLabel = [[UILabel alloc]init]; 
     nameLabel.textAlignment = UITextAlignmentLeft; 
     nameLabel.textColor = [UIColor colorWithRed:25/256.0 green:98/256.0  blue:52/256.0 alpha:1.0]; 
     costLabel = [[UILabel alloc]init]; 
     costLabel.textAlignment = UITextAlignmentLeft; 


     giftPicture = [[UIImageView alloc]init]; 

     [self.contentView addSubview:nameLabel]; 
     [self.contentView addSubview:costLabel]; 
     [self.contentView addSubview:giftPicture]; 

     costLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background4"]]; 
     nameLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background4"]]; 
    } 
    return self; 
} 
- (void)layoutSubviews { 
    [super layoutSubviews]; 

    CGRect contentRect = self.contentView.bounds; 
    CGFloat boundsX = contentRect.origin.x; 
    CGRect frame; 
    frame= CGRectMake(boundsX+10,10, 50, 50); 
    giftPicture.frame = frame; 

    frame= CGRectMake(boundsX+70 ,10, 350, 25); 
    nameLabel.frame = frame; 

    frame= CGRectMake(boundsX+70 ,40, 350, 18); 
    costLabel.frame = frame; 
} 
- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

@end 

List.m

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [array1 count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[CustomCell alloc] initWithFrame:CGRectZero]; 
    } 

    // Configure the cell... 


    NSString *cellValue = [array1 objectAtIndex:indexPath.row]; 
    //cell.textLabel.text = cellValue; // загаловок клетки 
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; // сделал чтобы клетка была со стрелочкой 

    cell.nameLabel.text = cellValue; 

    NSDictionary *mainDictionary = [[NSDictionary alloc]init]; 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSArray *languages = [defaults objectForKey:@"AppleLanguages"]; 
    NSString *currentLanguage = [languages objectAtIndex:0]; 
    if([currentLanguage isEqualToString:@"en"]) 
    { 
     mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animals" ofType:@"txt"]]; 
    } 
    NSEnumerator *enumerator = [mainDictionary objectEnumerator]; 
    id returnValue; 

    while ((returnValue = [enumerator nextObject])) 
    { 

     if([cell.nameLabel.text isEqualToString:[returnValue valueForKey:@"name"]]) 
     { 

      cell.animalPicture.image = [UIImage imageNamed:[returnValue valueForKey:@"icon"]]; 
      cell.costLabel.text = [NSString stringWithFormat:@"$%@ - $%@",[returnValue valueForKey:@"minAge"],[returnValue valueForKey:@"maxAge"]]; 

     } 
    } 
    return cell; 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 75; 
} 
@end 

난 그냥 때문에 호기심의 사용자 정의 셀이 비어 있도록 (라벨과 이미지를 제거)을 시도했으나 CustomCell.m : 다음은 내 코드입니다 그러나 그것은 아무것도 바뀌지 않았습니다. 제발 말해 줄래, 내가이 지연을 수정하려면 무엇을 확인해야합니까? 감사합니다.

+0

확실치 않지만,'CustomCell' 클래스와 관련이있을 수 있습니다. 각 셀에 여러보기가 포함되어 있으며 스크롤시 지연이 발생할 수 있습니다. – sooper

답변

4

네가 생각하는 각 테이블 셀에 대해 수행해야 할 작업이 많습니다 (시뮬레이터의 성능을 결코 신뢰할 수 없으며, 장치에서 동작하는 방식과 관련해서는 아무런 의미가 없습니다). 당신이 시도하고 시간을내어 정확히 뭐죠 찾아 낼 악기를 사용하지만 나는 그보고가 있다면 내가 말을해야합니다

그냥 스타터
NSDictionary *mainDictionary = [[NSDictionary alloc]init]; 

이가) 있기 때문에이 다음 줄의 무의미한 것와 b)처럼 보이는 이것을 유출하고 있습니다. 때마다이 파일은 성능 별 수 없습니다

mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animals" ofType:@"txt"]]; 

로드, 당신은 당신의 뷰 컨트롤러 대신 당신이 셀을 작성마다 좋아 한 번에이 데이터를 가져와야합니다.

NSEnumerator *enumerator = [mainDictionary objectEnumerator]; 
id returnValue; 

while ((returnValue = [enumerator nextObject])) 
{ 

    if([cell.nameLabel.text isEqualToString:[returnValue valueForKey:@"name"]]) 
    { 

여기 전체 덩어리는 재 작업 할 것입니다. 이것은 무딘, NSDictionary 잘못 사용하는 것입니다. 사전 적이 키와 값, 당신은 당신이 문자열 비교를 수행하여 원하는 값을 찾아 열거로 모든 것을 스스로를 스캔 할 필요가 없습니다 포함, 당신은

[mainDictonary objectForKey:@"thingYouWant"]; 

일 같은 것을 사용되어야 하는지를 당신은 사전의 60 개의 완전한 열거 형과 같다고 말했고 문자열 비교는 테이블 성능에 좋지 않을 수 있으며 필요하지 않습니다.

성능에 대한 확인 작업을 먼저 수행 한 다음 필요에 따라 재 작업을해야합니다. 성능 문제가 그 청크에 포함되어있을 것입니다.

+0

모든 조언을 주셔서 대단히 감사합니다! 예,이 코드로 많은 작업을하고 모든 "나쁜 것들"을 제거해야합니다. – SmartTree

+0

그래, 지연의 주된 이유는 내가 셀을 만들 때마다 파일을로드하고 있다는 것이 었습니다. 이제는 한 번만하고 tableView는 전혀 지연되지 않습니다! 고마워요! – SmartTree

4

iOS 앱의 성능 문제를 해결할 때 시뮬레이터가 아닌 기기를 신뢰합니다. 성능이 두 가지 방향으로 간다는 것을 보았습니다 (때로는 시뮬레이터에서는 더 좋았고 때로는 장치에서 더 좋았습니다). 기기에서 테스트 해보니 장기간에 걸쳐 많은 시간을 절약 할 수 있습니다.

지연되는 스크롤은 이미지 처리 나 다른 네트워크 활동으로 인해 스크롤 코드가 차단 된 결과 일 수 있습니다.

이러한 상황을 디버깅하기 위해 개인적으로 선호하는 것은 빈 테이블보기로 시작하는 것입니다. 수천 개의 셀이 있어도 스크롤이 원활해야합니다. 그런 다음 tableView 내용을 하나씩 추가하고 스크롤이 지연되는 시점에주의하십시오.

Apple's Lazy Table Images sample code 도울 수 있습니다.

+0

위대한, 고마워, 나는 그것을 시험 할 것이다. – SmartTree

4

-cellForRowAtIndexPath 메서드에서 초기화하는 작업이 모든 테이블 셀에 대해 UIImage입니다. 이 작업은 한 번만 수행하고 이미지를 캐시해야합니다.

+0

고마워, 나는 그것을 시도 할 것이다. – SmartTree

관련 문제