2012-01-14 14 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でセルを作成していますか?もしそうなら、2つのカスタムタイプのセルを作成し、 'currentCategory'に基づいて適切なものを返すことができます。 'UITableViewCell'をサブクラス化し、UI要素をプログラマチックに追加することで同じことができます。 –

+0

私は知っている、ありがとう)しかし、私はすでに私のアプローチを選んだ)ありがとう) –

答えて

3

私はちょうどcellForRowAtIndexPath memory managementで似たような質問に答えました。

基本的にはセルが再利用されるため、表示されるたびにサブビューがセルに追加され、時間の経過とともにビルドアップされます。

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

そして、ちょうど持っている:あなたは1つのレイアウトを持つセルが異なるレイアウトを必要とする、またはあなただけのこのロジックを取り除くことができ、細胞のために再利用されていない各セルのレイアウトに異なるCellIdentifierを使用することができますいずれかこの:

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

あなたの細胞を毎回再使用されることはありません、あなたは、彼らが使用された前回の内容をクリーンアップを心配する必要はありませんこの方法。

実際にあなたの質問に答えるには、セルのサブビューをループして、毎回それらを取り除くのに[view removeFromSuperview]と言うことができますが、それは良い解決策ではないと思います。

+0

ありがとうたくさん(ハンドシェイク) –

関連する問題