How to Select particular check Box in tableView which is inserted in table cell interface builder in iphone

后端 未结 1 1382
深忆病人
深忆病人 2021-01-27 07:25

I am doing a small concept on check box. Actually, i placed the check box image in the table cell.But i can\'t understand, how to select the particular check box and get the val

相关标签:
1条回答
  • 2021-01-27 08:09

    Are you adding a button on the RequestSongSelectingCell.xib file?

    When you add it on the tableView, you can set the tag property of it. And then you can retrieve tag in the buttonPressed method.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        NSLog(@"in table view cell for row at index");
    
       RequestSongSelectingCell *cell = (RequestSongSelectingCell *) [tableView dequeueReusableCellWithIdentifier:@"requestsingcell"];
            if (cell==nil) 
        {
    
            [[NSBundle mainBundle] loadNibNamed:@"RequestSongSelectingCell" owner:self options:nil];
                    NSLog(@"start");
            cell=requestingCell;
            NSLog(@"end");
        }
        cell.btn.tag = indexPath.row;
        // your customization
        return cell; 
    }
    
    -(IBAction)checkButtonPressed:(id)sender
    {
        UIButton * btn = (UIButton*)sender;
        int selected_index = btn.tag;    // This is your index of selected cell
    
        NSLog(@"check box button pressed");
    
        requestingCell.checkBoxButton.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"];
    
    }
    

    EDIT: To handle selection

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        NSLog(@"in table view cell for row at index");
    
       RequestSongSelectingCell *cell = (RequestSongSelectingCell *) [tableView dequeueReusableCellWithIdentifier:@"requestsingcell"];
            if (cell==nil) 
        {
    
            [[NSBundle mainBundle] loadNibNamed:@"RequestSongSelectingCell" owner:self options:nil];
                    NSLog(@"start");
            cell=requestingCell;
            NSLog(@"end");
        }
    //check if cell is already selected or not
            // if not selected, tag will be positive
            cell.btn.tag = indexPath.row;
            // if selected, tag will be negative
            cell.btn.tag = -indexPath.row;
            // your customization
            return cell; 
        }
    
    -(IBAction)checkButtonPressed:(id)sender
    {
    
    
        UIButton * btn = (UIButton*)sender;
        int selected_index = btn.tag;    // This is your index of selected cell
    
    
        //btn is the same btn image of which you need to change/toggle
        if(btn.tag > 0)     //its not selected
        {
                //set btn image as selected
                btn.tag = -(btn.tag);
                [btn setImage: forState:UIControlStateNormal];
        }
        else if(btn.tag < 0)     //its selected
        {
                //set btn image as unselected
                btn.tag = -(btn.tag);
                [btn setImage: forState:UIControlStateNormal];
        }
        NSLog(@"check box button pressed");
    
    //        requestingCell.checkBoxButton.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"];
    
        }
    
    0 讨论(0)
提交回复
热议问题