我正在制作一个我以复选框形式使用的应用。我想显示我正在尝试的文本标签中的复选框计数,但找不到可能的解决方案..这是我的项目链接https://www.dropbox.com/s/iu8fdz4y0ps4xye/checkboxwithTableview%202.zip?dl=0请审核它.. 以下是我的示例代码
- (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];
}
答案 0 :(得分:0)
在.h int checkBoxesCount;
中添加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];
}
问题已解决。我在最后测试了同样的东西。