【问题标题】:How to show count of checkboxes botton in a label如何在标签中显示复选框的数量
【发布时间】:2014-11-27 14:54:31
【问题描述】:

我正在制作一个应用程序,我在 uitableview 单元格中使用复选框。现在我被困在它上面我如何在标签中显示选定复选框的数量。就像我选中一个框,然后在标签中显示“选中的 1 个复选框”,如果我选中 2 个复选框,然后在标签中选中“选中的 2 个复选框”,将显示如何完成任何想法?下面是我处理复选框按钮的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *tableviewidentifier = @"cell";
    tablecellTableViewCell *cell= [self.activitiesTableView_ dequeueReusableCellWithIdentifier:tableviewidentifier];

    if(cell==nil)
    {
        cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
    }

    UILabel *valuedate = (UILabel *)[cell viewWithTag:11];
    UILabel *msg = (UILabel *)[cell viewWithTag:12];
    UILabel *date = (UILabel *)[cell viewWithTag:13];
    UILabel *time = (UILabel *)[cell viewWithTag:14];
    [valuedate setText:@"Demo"];
    [msg  setText:@"How are You?"];
    date.text=@"14/07/2014";
    time.text=@"A 08:16";


    // [cell.textLabel setText:activityModel.userName];
    valuedate.font=[UIFont fontWithName:@"SegoeUI" size:15];
    msg.font=[UIFont fontWithName:@"SegoeUI-light" size:10.0];
    date.font=[UIFont fontWithName:@"SegoeUI-light" size:9];
    time.font=[UIFont fontWithName:@"SegoeUI-light" size:9];



    if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]])
    {
         [cell.button setImage:[UIImage imageNamed:@"tick.png"] forState:UIControlStateNormal];

        //[cell.button setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];

    }
    else
    {
         [cell.button setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];

    }

    cell.button.tag=indexPath.row;
    [cell.button addTarget:self action:@selector(checkButton:) forControlEvents:UIControlEventTouchUpInside];



    return cell;
}

【问题讨论】:

    标签: ios uitableview checkbox


    【解决方案1】:
     viewDidLoad
    
     yourTableView.allowsmultiselection=YES;
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath]; 
       [cell.btn setBackgroundImage:[UIImage imageNamed:@"selected-24.png"] forState:UIControlStateNormal];
       NSArray *arr=[tableView indexPathsForSelectedRows];
       lbl.textLabel.text=[NSString stringWithFormat:@"%d",[arr count] ];
    }
    in deselect 
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath]; 
       [cell.btn setBackgroundImage:[UIImage imageNamed:@"unselected-24.png"] forState:UIControlStateNormal];
       NSArray *arr=[tableView indexPathsForSelectedRows];
       lbl.textLabel.text=[NSString stringWithFormat:@"%d",[arr count] ];
     }
    

    【讨论】:

    • 像 Nsinteger i 一样增加计数器;然后是 i++?
    • 我如何存储计数??
    • 静态 int i=0; @implementation YourViewController 之后
    • 我如何将计数存储在 i 变量中?
    • 这是如何将复选框数量存储在此变量中的主要内容?
    【解决方案2】:

    为所选按钮获取一个 NSMutableArray。当您选择特定按钮时,将该 indexpath.row 值添加到数组中。当您取消选择该按钮时,检查该数组中是否存在 indexpath.row 值,如果是,则找到该对象的索引并从数组中删除该对象。

    if([array containsObject:@"value"]){
            int n=[array indexOfObject:@"value"];
            [array removeObjectAtIndex:n];
    
        }
    

    现在检查数组的计数为[数组计数];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 2016-06-29
      相关资源
      最近更新 更多