2011-04-21 5 views
1

Je travaillais avec le UITableView et j'avais une case à cocher liée à chaque ligne d'éléments.case à cocher dans la table UITableView

Lors d'un événement de clic sur un bouton, il doit générer une liste d'éléments cochés dans le UITableView.

Je ne suis pas en mesure de le faire, S'il vous plaît, aidez-moi

Répondre

0

Passez par les liens SO.

Checkbox cell in a table view: User can't check it

checkbox button in table view in iphone

Pour en savoir plus lire Apple Doc pour gérer la sélection dans UITableViewCell.

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/ManageSelections/ManageSelections.html

ÉDITÉE:

Utilisez la propriété tag de UIButton,

#define KON 1 //For checked state 
#define KOFF 0 //For Unchecked state 

-(void) myButtonAction:(id) sender 
{ 
    UIButton* myButton = (UIButton*) sender; 

    if(myButton.tag == KON) 
    { 
     myButton.tag = KOFF; 
     //Change the button image 
    } 
    else 
    { 
     myButton.tag = KON; 
     //Change the button image 

    } 
} 
+0

Comment créer 3 case à cocher pour 3 lignes dans une table – sujay

+0

@sameer: ​​Ajouter un UIButton dans chaque cellule, – Jhaliya

+0

@sameer: ​​Accepter si ma réponse vous aide. – Jhaliya

0

Exemple, cliquez sur la première ligne est utilisée pour changer pour d'autres lignes. Cliquez sur les autres lignes seulement changer lui-même. arrCheckbox est un tableau utilisé pour stocker la vérification de toutes les lignes.

- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //NSLog(@"AV--%s", __func__); 
    HistoryCell2 *cell = [tableview dequeueReusableCellWithIdentifier:CellId]; 

    if (cell == nil) { 
     NSLog(@"Create new row"); 
     [tableview registerNib:[UINib nibWithNibName:@"HistoryCell2" bundle:nil] forCellReuseIdentifier:CellId]; 
     cell = [tableview dequeueReusableCellWithIdentifier:CellId]; 
    } 
    cell.tag = indexPath.row; 

    BOOL checked = [[arrCheckbox objectAtIndex:indexPath.row] boolValue]; 
    UIImage *image = (checked) ? [UIImage imageNamed:@"cb_on.png"] : [UIImage imageNamed:@"cb_off.png"]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    CGRect frame = CGRectMake(1.0, 1.0, 29, 29); 
    button.frame = frame; // match the button's size with the image size 
    button.tag = indexPath.row; 
    [button setBackgroundImage:image forState:UIControlStateNormal]; 

    // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet 
    [button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:button]; 

    return cell; 
} 

- (void) checkButtonTapped:(id)sender 
{ 
    NSLog(@"%s", __func__); 
    UIButton *tappedButton = (UIButton*)sender; 
    int index = (int)tappedButton.tag; 
    NSLog(@"Row button: %d", index); 

    if(index > 0) 
    { 
     BOOL checked = [[arrCheckbox objectAtIndex:index] boolValue]; 
     [arrCheckbox removeObjectAtIndex:index]; 
     [arrCheckbox insertObject:(checked) ? @"0":@"1" atIndex:index]; 

     UIImage *newImage = (checked) ? [UIImage imageNamed:@"cb_off.png"] : [UIImage imageNamed:@"cb_on.png"]; 
     [tappedButton setBackgroundImage:newImage forState:UIControlStateNormal]; 
    } 
    else{ 
     //UITableView *tableview = (UITableView *)[[tappedButton superview] superview]; 
     UIImage *newImage; 
     BOOL checked = [[arrCheckbox objectAtIndex:0] boolValue]; 
     for(int i=0; i<[arrCheckbox count]; i++) 
     { 
      //NSLog(@"Row: %d------", i); 
      [arrCheckbox removeObjectAtIndex:i]; 
      [arrCheckbox insertObject:(checked) ? @"0":@"1" atIndex:i]; 

      UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
      for(UIView *subview in cell.contentView.subviews) 
      { 
       if([subview isKindOfClass: [UIButton class]]) 
       { 
        //NSLog(@"Modify button"); 
        tappedButton = (UIButton*)subview; 
        //[subview removeFromSuperview]; 
        newImage = (checked) ? [UIImage imageNamed:@"cb_off.png"] : [UIImage imageNamed:@"cb_on.png"]; 
        [tappedButton setBackgroundImage:newImage forState:UIControlStateNormal]; 
       } 
      } 
     } 
    } 
}