UITableview iPhone中的多个复选框

时间:2012-05-05 11:41:56

标签: iphone uitableview

我是iOS新手 我在tableview中创建动态按钮 我在按钮上设置了一个图像以供检查和取消选中 我要做的是当我点击(或检查)按钮时,indexpath行上的数据将添加到我的数组中。 如果我取消选择它,数据将从我的数组中删除。请帮帮我

-(void)btnForCheckBoxClicked:(id)sender
{

UIButton *tappedButton = (UIButton*)sender;

indexForCheckBox= [sender tag];


if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkbox_unchecked.png"]]) 
{

    [sender  setImage:[UIImage imageNamed: @"checkbox_checked.png"] forState:UIControlStateNormal];
    strinForCheckBox= [ApplicationDelegate.ArrayForSearchResults objectAtIndex:indexForCheckBox];
    [arrForCheckBox addObject:strinForCheckBox];
    NSLog(@"Sender Tag When Add %d", indexForCheckBox);
    NSLog(@"Array Count Check Box %d",[arrForCheckBox count]);

}

else
{

        [sender setImage:[UIImage imageNamed:@"checkbox_unchecked.png"]forState:UIControlStateNormal];
        [arrForCheckBox removeObjectAtIndex:indexForCheckBox];
        NSLog(@"Sender Tag After Remove %d", indexForCheckBox);
        NSLog(@"Array Count Uncheck Box %d",[arrForCheckBox count]);


   }


}

4 个答案:

答案 0 :(得分:2)

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([selectedRowsArray containsObject:[contentArray objectAtIndex:indexPath.row]) {
        cell.imageView.image = [UIImage imageNamed:@"checked.png"];
    }
    else {
       cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];
    }
    UITapGestureRecogniser *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleChecking:)
    [cell.imageView addGestureRecognizer:tap];
    [tap release];

    cell.textLabel.text = [contentArray objectAtIndex];
    return cell;
}

- (void) handleChecking:(UITapGestureRecognizer *)tapRecognizer {
    CGPoint tapLocation = [tapRecognizer locationInView:self.tableView];
    NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];

    if (selectedRowsArray containsObject:[contentArray objectAtIndex:tappedIndexPath.row]) {
        [selectedRowsArray removeObject:[contentArray objectAtIndex:tappedIndexPath.row]];
    }
    else {
        [selectedRowsArray addObject:[contentArray objectAtIndex:tappedIndexPath.row]];
    }
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation: UITableViewRowAnimationFade];
}

答案 1 :(得分:1)

我认为这个问题已经回答了一段时间了!先检查一下然后告诉我们这是不是您要找的?

How to add checkboxes to UITableViewCell??

image http://img208.yfrog.com/img208/6119/screenshotkmr.png

答案 2 :(得分:0)

您可以在按钮点击方法中添加此项。

首先设置Bool值,在一次单击时设置为yes,否则在第二次单击时以及第一次单击时使用array addobject属性和其他单击removeobject属性在数组中添加该索引值。

-(void)list
{

if(isCheck==YES)
{
    //add array object  here
    isCheck=NO;
}
else if(isCheck==NO)
{
  //remove array object here
   isCheck=YES;
}

}

答案 3 :(得分:0)

我找到了另一种方式。从@The Saad提取的代码并且,我刚刚使用键/值对修改为有用的代码,如下面的代码 -

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([[selectedRowsArray objectAtIndex:indexPath.row] containsObject:@"YES"])
        cell.imageView.image = [UIImage imageNamed:@"checked.png"];
    else
       cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];

    UITapGestureRecogniser *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleChecking:)
    [cell.imageView addGestureRecognizer:tap];
    [tap release];

    cell.textLabel.text = [contentArray objectAtIndex];
    return cell;
}

- (void) handleChecking:(UITapGestureRecognizer *)tapRecognizer {
    CGPoint tapLocation = [tapRecognizer locationInView:self.tableView];
    NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];

    if ([[selectedRowsArray objectAtIndex:tappedIndexPath.row] containsObject:@"YES"])
        [selectedRowsArray replaceObjectAtIndex:tappedIndexPath.row withObject:[NSSet setWithObject:@"NO"]];
    else
        [selectedRowsArray replaceObjectAtIndex:tappedIndexPath.row withObject:[NSSet setWithObject:@"YES"]];

    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation: UITableViewRowAnimationFade];
}

添加selectedRowsArray的对象,如下所示,计算self.tableView的行数。因此,您需要添加bool值,如下所示

for ( int i = 0; i < tableViewRowCount; i++ ) { // tableViewRowCount would be your tableView's row count
    ....
    ....
    ....
    [selectedRowsArray addObject:[NSSet setWithObject:@"NO"]];
    ....
    ....
}

希望这有帮助!干杯!

<强>更新

使用更新后的代码,您无需保持NSMutableDictionary的键/值对过度使用。 NSSet为您提供了更好的方法。