2011-03-14 78 views
0

请看下面的代码片段。我将复选框放入我的单元格中,我可以选中或取消选中该复选框。但是当我滚动表格视图复选框的复选标记正在隐藏。
我的意思是我通过设置图像设置复选标记。帮我复选框的复选标记隐藏起来

请有人帮我解决这个问题。

谢谢你的时间。

// Customize the appearance of table view cells. 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) 
     { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"en"] autorelease]; 

     cb = [[UIButton alloc] initWithFrame:CGRectMake(5,10, unselectedImage.size.width, unselectedImage.size.height)]; 

    [cb setImage:unselectedImage forState:UIControlStateNormal]; 
    [cb setImage:selectedImage forState:UIControlStateSelected]; 
    [cb addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown]; 
    [cell.contentView addSubview:cb]; 

    } 

    if (tableView == myTableView) 
    { 
     titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 150, 35)]; 
     titleLabel.font = [UIFont boldSystemFontOfSize:13]; 
     titleLabel.textColor = [UIColor blackColor]; 
     [cell.contentView addSubview:titleLabel]; 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

     return cell; 
    } 

回答

1

here

两者如果我理解的TableView概念正确,你需要保存对号设置别的地方。如果您的单元格被滚动出视图,TableViewController可能会释放该单元格。然后,当您被要求在您的委托中重新创建单元格时,您必须以与以前相同的状态恢复它。

0

DidSelectRowAtindexPath

if ([set containsObject:indexPath]) 
{ 
    [set removeObject:indexPath]; 
} 
else 
{ 
    [set addObject:indexPath]; 
} 

CellForRpwAtIndexPath

if ([set containsObject:indexPath]) 
{ 
    cell.imgView.image=[UIImage imageNamed:@"chealBox1.png"]; 
} 
else 
{ 
    cell.imgView.image=[UIImage imageNamed:@"chealBox2.png"]; 
} 

这里设置为NSMutableSet,你必须作出财产和viewDidLoad中合成或Alloc,以及...肯定会工作。 ..