2012-01-14 6 views
0

어떤 범주를 선택했는지에 따라 다른 하위보기가있는 셀을 갖게 될 것입니다. 새로운 카테고리를 선택하고 데이터 등을 새로 고치지 만, 다른 하위 뷰가있는 새 셀을 표시하는 대신 뷰를 다른 카테고리로 전환 할 때보기가 서로 놓이게됩니다. 어떻게 수정합니까? 여기 내 코드는 다음과 같습니다 또한셀에 다른보기를 넣으려면 UITableView의 셀을 지우십시오.

//cell for row at indexPath  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]; 
    } 

    if ([currentCategory isEqualToString:@"Projects"]) 
    { 
     Project *pr=[projectsArray objectAtIndex:indexPath.row]; 
     NSLog(@"Project ID %i, ProjectName %@", pr.ident, pr.projectName); 

     UILabel *nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 100)]; 
     nameLabel.text=pr.projectName; 

     UIImageView *iv=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 192)]; 
     iv.image=pr.baseImage; 
     [cell addSubview:iv]; 
     [cell addSubview:nameLabel]; 
    } 
    else if ([currentCategory isEqualToString:@"Glossaire"]) 
    {    
     Glossaire *gl=[glossaireArray objectAtIndex:indexPath.row]; 
     UILabel *nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 45)]; 
     nameLabel.font=[UIFont fontWithName:@"Arial" size:25.0f]; 
     nameLabel.text=gl.glossaireName; 
     nameLabel.backgroundColor=[UIColor redColor]; 
     UILabel *introLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 50, 200, 50)]; 
     introLabel.font=[UIFont fontWithName:@"Arial" size:16.0f]; 
     introLabel.text=gl.intro; 
     introLabel.backgroundColor=[UIColor redColor]; 
     UILabel *descriptionLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 100, 350, 100)]; 
     descriptionLabel.font=[UIFont fontWithName:@"Arial" size:16.0f]; 
     descriptionLabel.text=gl.description; 
     descriptionLabel.backgroundColor=[UIColor redColor]; 
     NSLog(@"Glossaire ID: %i, NAME: %@ INTRO: %@ Description %@", gl.ident, gl.glossaireName, gl.intro, gl.description); 
     [cell addSubview:nameLabel]; 
     [cell addSubview:introLabel]; 
     [cell addSubview:descriptionLabel];    
    } 

    return cell; 
} 
//And switching between categories 
- (IBAction)viewProjects:(id)sender 
{ 
    [email protected]"Projects"; 
    projectsArray=[dbm fetchProjectsSummary]; 
    [mainTable reloadData];    
} 

- (IBAction)viewGlossaire:(id)sender 
{ 
    [email protected]"Glossaire"; 
    glossaireArray=[dbm fetchGlossaireSummary]; 
    [mainTable reloadData]; 
} 

들이 재사용 식별자가되지 않습니다 말, 그것의 새로운 버전은 무엇입니까? 감사!

+0

질문을 편집하고 제공된 코드의 형식을 지정하십시오. –

+0

IB로 ​​세포를 만드십니까? 그렇다면 두 개의 사용자 정의 유형의 셀을 작성하여 'currentCategory'에 따라 올바른 유형의 셀을 리턴 할 수 있습니다. 'UITableViewCell'을 서브 클래스 화하고 UI 요소를 프로그램 적으로 추가하여 동일한 작업을 수행 할 수 있습니다. –

+0

알아, 고마워)하지만 난 이미 내 접근 방식을 선택했습니다) 덕분에 –

답변

3

방금 ​​cellForRowAtIndexPath memory management과 비슷한 질문에 답했습니다.

기본적으로 셀이 다시 사용되므로 표시 할 때마다 셀에 하위보기를 추가하고 시간이 지남에 따라 셀을 작성합니다. 당신은 너무 한 레이아웃 셀이 다른 레이아웃을 필요로하는 세포에 재사용되지 않은 각 셀 레이아웃에 대해 다른 CellIdentifier을 사용할 수 중 하나, 또는 당신은이 논리를 제거 할 수 :

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]; 
} 

그리고 단지가 있습니다 이 :

UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil]; 

당신의 세포마다 다시 사용되지 않습니다 그리고 당신은 그들이 사용 된 마지막에서 내용을 정리에 대해 걱정할 필요가 없습니다 그런 식으로.

실제로 질문에 대답하려면 셀 서브 뷰를 반복하고 매번 [view removeFromSuperview]이라고 말하면 좋겠지 만 좋은 솔루션이라고 생각하지는 않습니다.

+0

고마워 (핸드 셰이크) –

관련 문제