0

我是 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

4 回答 4

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];
}
于 2012-05-05T11:54:41.357 回答
1

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

如何向 UITableViewCell 添加复选框?

图片 http://img208.yfrog.com/img208/6119/screenshotkmr.png

于 2012-05-05T11:48:48.547 回答
0

您可以在按钮单击方法中添加它。

首先设置布尔值,单击时设置为“是”,第二次单击设置为否,当您第一次单击时,使用数组 addobject 属性和其他单击 removeobject 属性在数组中添加该索引值。

-(void)list
{

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

}
于 2012-05-05T11:48:24.307 回答
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为您提供更好的方法。

于 2014-01-29T10:01:02.680 回答