Ios 复选框计数未正确显示
我正在制作一个应用程序,在其中我以复选框的形式使用。我想显示文本标签中的复选框数,我正在尝试,但找不到可能的解决方案。。这是我的项目链接,请查看。。 下面是我的示例代码Ios 复选框计数未正确显示,ios,uitableview,Ios,Uitableview,我正在制作一个应用程序,在其中我以复选框的形式使用。我想显示文本标签中的复选框数,我正在尝试,但找不到可能的解决方案。。这是我的项目链接,请查看。。 下面是我的示例代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *tableviewidentifier = @"cell"; Tab
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *tableviewidentifier = @"cell";
TablecellTableViewCell *cell= [self.tblvie dequeueReusableCellWithIdentifier:tableviewidentifier];
if(cell==nil)
{
cell = [[TablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
}
cell.lblImage.layer.cornerRadius=3.0f;
cell.lblImage.layer.borderWidth=1.0f;
if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]])
{
[cell.buttonCheckbox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
// [cell.buttonCheckbox setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
}
else
{
//[cell.buttonCheckbox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
[cell.buttonCheckbox setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
}
cell.buttonCheckbox.tag=indexPath.row;
[cell.buttonCheckbox addTarget:self action:@selector(checkButton:) forControlEvents:UIControlEventTouchUpInside];
cell.lblText.text=[self.lblArray objectAtIndex:indexPath.row];
return cell;
}
-(IBAction)checkButton:(UIButton *)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tblvie];
NSIndexPath *indexPath = [self.tblvie indexPathForRowAtPoint:buttonPosition];
if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]]) {
[self.checkimageArray removeObject:[self.lblArray objectAtIndex:indexPath.row]];
self.titleLabel.text=@"";
}
else {
[self.checkimageArray addObject:[self.lblArray objectAtIndex:indexPath.row]];
NSArray *arr=[self.tblvie indexPathsForSelectedRows];// here i am declaring an array but array is coming nil
self.titleLabel.text=[self.lblArray objectAtIndex:indexPath.row];
}
[self.tblvie reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];
}
在.h
Int checkboxescont中添加Int变量代码>
在ViewDidLoad中初始化它,如checkBoxesCount=0代码>
然后像下面这样更新您的方法
-(IBAction)checkButton:(UIButton *)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tblvie];
NSIndexPath *indexPath = [self.tblvie indexPathForRowAtPoint:buttonPosition];
if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]]) {
[self.checkimageArray removeObject:[self.lblArray objectAtIndex:indexPath.row]];
checkBoxesCount = checkBoxesCount - 1;
self.titleLabel.text=[NSString stringWithFormat:@"Count %d",checkBoxesCount];
}
else {
[self.checkimageArray addObject:[self.lblArray objectAtIndex:indexPath.row]];
// self.titleLabel.text=[self.lblArray objectAtIndex:sender.tag];
checkBoxesCount = checkBoxesCount + 1;
self.titleLabel.text=[NSString stringWithFormat:@"Count %d",checkBoxesCount];
}
[self.tblvie reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];
}
问题解决了。我也测试了同样的代码。我尝试了你的代码,当我点击复选框时,它显示了复选框1,2。。。等等,你到底想展示什么?选中的复选框总数?是的,选中的复选框总数…有两种方法,要么获取所有复选框并选中哪个复选框,要么将总数显示到标签。花钱多的另外一个简单的方法是,在方法checkButton add+1中选中复选框时,取int count。最后在标签上显示总计。您将如何提供一些资料?