2011-01-24 4 views
3

UITableView가 있고 테이블을 표시 할 사용자 지정 셀을 만들었습니다. 6 개의 UILables가 표시되어 있고 표시 할 레코드는 20 개이지만 스크롤 할 때 매우 느립니다. 다음과 같습니다 : cellForRowAtIndexPath : 모든 것들을 내가 여기 일 (NSDateFormatter, CGMakeRect, floatToStringFormat하고 있기 때문에 스크롤에 느린 것을 실행하기위한UITableView 사용자 지정 셀 매우 느림

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

    static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; 

    HistoryCell *cell = (HistoryCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 

    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HistoryCell" owner:nil options:nil]; 

     for (id oneObject in nib) 
      if ([oneObject isKindOfClass:[UITableViewCell class]]) 
       cell = (HistoryCell *) oneObject; 
    } 

    NSArray *object; 
    object = [cours objectForKey: [NSString stringWithFormat:@"%d", indexPath.section]]; 
    History *rowData = [object objectAtIndex:indexPath.row]; 

    if (rowData.month == 99) { 
     cell.hour.frame = CGRectMake(10, 0, 135, 35); 
     cell.data.hidden = YES; 
     cell.hour.textColor = [UIColor blackColor]; 
     cell.hour.font = [UIFont fontWithName:@"Verdana" size:17]; 
    } else { 
     cell.data.hidden = NO; 
     cell.hour.frame = CGRectMake(10, 16, 135, 19); 
     cell.hour.textColor = [UIColor grayColor]; 
     cell.hour.font = [UIFont fontWithName:@"Verdana" size:12]; 
    } 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"d (EEEE)"]; 
    [formatter setLocale:self.selectedLanguageLocale]; 
    NSString *stringFromDate = [formatter stringFromDate:rowData.data]; 
    [formatter release]; 

    cell.data.text = stringFromDate; 
    cell.hour.text = rowData.ora; 

    float Var1 = [rowData.Var2 floatValue]; 
    float Var2 = [rowData.Var2 floatValue]; 

    cell.R1.text = [self floatToStringFormat: [rowData.R1 floatValue]]; 
    cell.R2.text = [self floatToStringFormat: [rowData.R2 floatValue]]; 

    if (Var1 <= 0) { 
     cell.Var1.textColor = [UIColor greenColor]; 
    } else { 
     cell.Var1.textColor = [UIColor redColor]; 
    } 
    if (Var2 <= 0) { 
     cell.Var2.textColor = [UIColor greenColor]; 
    } else { 
     cell.Var2.textColor = [UIColor redColor]; 
    } 
    cell.Var1.text = [self floatToStringFormat:Var1]; 
    cell.Var2.text = [self floatToStringFormat:Var2]; 

    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

이유 ...있는 tableView -

이 어떻게 내입니다) 또는 재사용 세포에 문제가 있습니까?

floatToStringFormat 4 개 소수에 다수를 포맷 함수이다 : 첫째

- (NSString *)floatToStringFormat:(float)number{ 
    NSNumberFormatter *myFloat = [[NSNumberFormatter alloc] init]; 
    [myFloat setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
    [myFloat setNumberStyle:NSNumberFormatterDecimalStyle]; 
    [myFloat setRoundingMode:NSNumberFormatterRoundHalfUp]; 
    [myFloat setMinimumFractionDigits:4]; 
    [myFloat setMaximumFractionDigits:4]; 
    NSString *res = [myFloat stringFromNumber:[NSNumber numberWithFloat:number]]; 
    [myFloat release]; 
    return res; 
} 

답변

6

포맷터 객체를 만들고 설정하는 것은 실제로 비용이 많이 드는 작업이므로 실제로는 각 함수 호출에서 같기 때문에 포맷터 객체를 다시 사용하기 시작합니다. 그래서 그 데이터 소스 클래스의 정적 변수 또는 인스턴트 변수를 확인하고 다음과 같은 방식으로 작성 중 하나

//static variable case 
NSDateFormatter *formatter = nil; 
if (!formatter){ 
    formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"d (EEEE)"]; 
    [formatter setLocale:self.selectedLanguageLocale]; 
} 
NSString *stringFromDate = [formatter stringFromDate:rowData.data]; 
... 
3

두 개의 서로 다른 식별자를 사용 : CustomCellIdentifierBanciHistoryCellIdentifier하고있다.

둘째, 새 셀이 표시 될 때마다 NSArray *object; 이후에 모든 작업을 실제로 수행해야합니까? 그렇지 않으면 블록을 if (cell == nil) { 블록으로 이동해야합니다.

+0

사실은 진짜 하나는 BanciHistoryCellIdentifier입니다.하지만 여기서는 CustomCellIdentifier로 이름을 변경하여 이름을 바꿀 수 없게되었습니다. – CristiC

2

내 경험으로 보면 세 가지 이상의 하위 뷰 (장치 및 뷰에 따라 다르지만)가있는 경우 테이블 뷰 셀 그리기가 상당히 느려집니다. 직접 drawRect에 내용을 그려보십시오 : 대신 하위보기를 사용하여,이 일을 빨리해야합니다.

-1

은 당신이 인터페이스 빌더에서 CellIdentifier을 설정 함을? 코드에서 사용하는 것과 정확히 일치해야합니다. 펜촉에서 셀을로드하는 중단 점을 설정하고 스크롤 할 때 셀을 재사용하는지 확인하십시오. 여기를 뭐

0

:

if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HistoryCell" owner:nil options:nil]; 

     for (id oneObject in nib) 
      if ([oneObject isKindOfClass:[UITableViewCell class]]) 
       cell = (HistoryCell *) oneObject; 
    } 

이동이 제대로이 작업을 수행하는 방법에 대한 documentation를 참조하십시오. 둘째, 날짜와 숫자를 문자열로 변환하는 데 너무 오래 걸리는 경우 문자열 값을 대신 저장하고 수정해야 할 때 값으로 변환하십시오.