2011-03-14 79 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,以及...肯定會工作。 ..