【问题标题】:How to Select particular check Box in tableView which is inserted in table cell interface builder in iphone如何在tableView中选择特定的复选框,该复选框插入到iphone的表格单元界面构建器中
【发布时间】:2026-02-21 17:00:01
【问题描述】:

我正在做一个关于复选框的小概念。实际上,我将复选框图像放在表格单元格中。但是我不明白,如何选择特定的复选框并在表格视图中获取单元格的值,任何人都可以帮助我解决这个问题。我会提供带有编码的表格视图的屏幕截图,

- (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");
    }

    NSDictionary *dict=[self.array objectAtIndex:indexPath.row];

    NSString *artistname=[dict objectForKey:@"artist"];
    NSLog(@"artistname is %@",artistname);

    cell.artistName.text=artistname;

    NSString *songtitle=[dict objectForKey:@"title"];
    NSLog(@"songtitle is %@",songtitle);
    cell.artistTitle.text=songtitle;

    return cell;
 }

-(IBAction)checkButtonPressed:(id)sender
{
    NSLog(@"check box button pressed");

    requestingCell.checkBoxButton.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"];

}

感谢和问候,

girish

【问题讨论】:

    标签: iphone button tableview tablecell


    【解决方案1】:

    您是否在 RequestSongSelectingCell.xib 文件上添加按钮?

    在tableView上添加的时候,可以设置它的tag属性。然后你可以在 buttonPressed 方法中检索标签。

    - (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"];
    
    }
    

    编辑:处理选择

    - (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"];
    
        }
    

    【讨论】:

    • 嗨 vaibhav 感谢您对它的工作的回应,但我遇到了一个问题,即当我点击按钮时,图像没有改变(勾号)。您能指导我这样做吗?
    • 这很棘手,我们在标签中维护单元格的索引,所以不能将它用于选中或未选中的值。您可以将标签设置为正或负。对未选中说正面,对选中说否定。这可能是处理此问题的一种方法。我将编辑相同的代码。
    • 嗨 vaibhav 感谢它的工作。你能把你的邮件 ID 转发给我吗。我的邮件 ID 是 girishpotnuru@gmail.com
    最近更新 更多