2009-08-03 2 views
0

테스트하기 위해 코드를 간소화했으며 여전히 전화로 내 메모리 사용량이 테이블의 속도가 느려지는 지점까지 올라간다.왜 테이블 셀이 출시되지 않습니까?

누군가 내가 여기서 잘못하고있는 것을 말해 줄 수 있습니까?

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 40; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 100; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellID = @"Cell"; 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease]; 
    } 
    UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)]; 
    l.font=[UIFont boldSystemFontOfSize:15]; 
    l.textColor=[UIColor whiteColor]; 
    l.backgroundColor=[UIColor blackColor]; 
    [email protected]"Just some randoom text here"; 
    [cell.contentView addSubview:l]; 
    [l release]; 

아차 . 코드 붙여 넣기가 너무 잘 작동하지 않았습니다. 여기에 바로 붙여 넣기입니다 :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 40; 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 100; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellID = @"Cell"; 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease]; 
    } 
    UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)]; 
    l.font=[UIFont boldSystemFontOfSize:15]; 
    l.textColor=[UIColor whiteColor]; 
    l.backgroundColor=[UIColor blackColor]; 
    [email protected]"Just some randoom text here"; 
    [cell.contentView addSubview:l]; 
    [l release]; 
     return cell; 
} 

답변

0

당신은있는 UITableViewCell 인스턴스를 재활용하고,하지만 당신은 여전히 ​​모든 행에 대해 새로운 UILabel의 인스턴스를 생성하고 각각의 모든 세포에 추가하고 있습니다. 이것은 메모리 사용량의 출처입니다. 이런 식으로 뭔가를 시도 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellID = @"Cell"; 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease]; 
     UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)]; 
     l.font=[UIFont boldSystemFontOfSize:15]; 
     l.textColor=[UIColor whiteColor]; 
     l.backgroundColor=[UIColor blackColor]; 
     [email protected]"Just some randoom text here"; 
     [cell.contentView addSubview:l]; 
     [l release]; 
    } 
    return cell; 
} 
1

당신은이 같은 패턴을 따라하기를 원할 것입니다 : 모든있는 UITableViewCell 자체 표준 UILabel의 (cell.textLabel)을 가지고 있기 때문에

#define kTagMyLabel 1 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellID = @"Cell1"; 
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID]; 
    UILabel * l; 
    if (cell == nil) { 
    // create the cell 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease]; 

    // perform setup/functions that are common to all cells 
    l = [[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)] autorelease]; 
    l.font=[UIFont boldSystemFontOfSize:15]; 
    l.textColor=[UIColor whiteColor]; 
    l.backgroundColor=[UIColor blackColor]; 
    l.tag = kTagMyLabel ; 
    [cell.contentView addSubview:l]; 
    } 
    else 
    { 
    // find the label we previously added. 
    l = (UILabel*)[cell viewWithTag:kTagMyLabel]; 
    } 

    // now set up the cell specific to this indexPath 
    [email protected]"Just some random text here"; 
    return cell; 
} 
+0

불행히도이 솔루션을 사용하면 메모리가 고정됩니다 (직선 복사 및 붙여 넣기). 아마도 테이블 뷰가 배치되는 방식 일 수 있습니다. IB에서 tabbar 컨트롤러를 생성하고 IB에서 뷰 컨트롤러도 할당합니다. 그래서 자동 응답 풀이 절대로 출시 될 기회가 없습니까? 더 좋은 방법은 무엇일까요? 그리고 NSAutoreleasePool을 메인이 아닌 다른 곳에 설치해야합니까? 그렇다면,보기 자체가 탭바 위치에있는 것이 해제되지 않는다면? –

+0

이것이 무엇인지는 모르지만 시뮬레이터에서 메모리가 올바르게 해제됩니다. –

+0

모든 UI 이벤트 후에 풀이 소모됩니다. 정확히 * 출시되지 않은 것은 무엇입니까? 즉 언제 메모리가 소모되는 걸까요? 긴 항목 목록을 스크롤 한 다음 viewcontroller에서 다시 빠져 나올 때입니까? 다른 곳에서는 누수가 있습니다. – Jason

0

을, 당신이 정말로 별도의 라벨 것을 필요합니까 contentView에 추가 되었습니까? 사용자 지정 셀이 필요하면 UITableViewCell의 하위 클래스를 고려할 수 있습니다.

+0

내가 준 예제는 페어링 된 다운 버전이었습니다. 제 실제 코드에서는 UITableviewCell을 하위 클래스로 만들고 여러 필드와 이미지를 추가합니다. –

관련 문제