2012-01-14 87 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

请编辑您的问题并格式化您提供的代码。 – 2012-01-14 11:16:27

+0

你用IB来创建单元格吗?如果是的话,你可以创建两个自定义类型的单元格,并根据'currentCategory'返回正确的单元格。通过继承'UITableViewCell'并以编程方式添加UI元素可以实现同样的效果。 – 2012-01-14 11:50:04

+0

我知道,谢谢),但我选择了 – 2012-01-14 14:37:53

回答

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

非常感谢我已经办法)谢谢)(握手) – 2012-01-14 12:43:48

相关问题