如何在 iphone 中插入表格单元界面生成器的 tableView 中选择特定复选框

发布于 2024-10-25 07:17:13 字数 1276 浏览 6 评论 0原文

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

- (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

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 value of the cell in table view, Can any body help me by solving this problem.I will provide the screen shots of table view with coding,

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

}

enter image description here

Thanks and regards,

girish

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2024-11-01 07:17:13

您是否在 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"];

    }

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

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文