ios - 如何在单击按钮时选择所有 uitableview 单元格

标签 ios uitableview

我正在制作一个应用程序,其中我在uitableview单元格中使用复选框形式的按钮。我正在更改该复选框选择和取消选择状态上的图像。现在我希望当我单击另一个按钮时, TableView 的所有单元格都将被选择,并且所有单元格复选框的图像都将更改?下面是我的示例代码是如何完成的

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.lblArray.count;

}



- (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;
    cell.backgroundColor=[UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];



    if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]])
    {

        [cell.buttonCheckbox setImage:[UIImage imageNamed:@"check_box_ok.png"]
                     forState:UIControlStateNormal];

    }
    else
    {
        [cell.buttonCheckbox setImage:[UIImage imageNamed:@"uncheck.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]];
                self.titleLabel.text=[self.lblArray objectAtIndex:sender.tag];

    }
    [self.tblvie reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];


}

这是我的按钮,我单击该按钮,然后表格 View 的所有单元格都会选择,并且所有单元格的图像都会更改,但它不起作用

- (IBAction)selectbtns:(id)sender {
    static NSString *tableviewidentifier = @"cell";
    TablecellTableViewCell *cell= [self.tblvie dequeueReusableCellWithIdentifier:tableviewidentifier];
    [cell.buttonCheckbox setImage:[UIImage imageNamed:@"check_box_ok.png"]
                         forState:UIControlStateNormal];


   }

最佳答案

NSMutableArray *totalcheckmarkArray; //add on NSMutablearray in globally

- (void)viewDidLoad    {

totalcheckmarkArray =[[NSMutableArray alloc]init];

 for (int i=0; i<[self.lblArray count]; i++) // Number of Rows count
{
 [self.totalcheckmarkArray addObject:@"NO"];
}

}



-(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.totalcheckmarkArray objectAtIndex:indexPath.row] isEqualToString:@"NO"])
{

    [cell.buttonCheckbox setImage:[UIImage imageNamed:@"check_box_ok.png"] forState:UIControlStateNormal];

}

else if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]])
{

    [cell.buttonCheckbox setImage:[UIImage imageNamed:@"check_box_ok.png"] forState:UIControlStateNormal];

}
else
{
    [cell.buttonCheckbox setImage:[UIImage imageNamed:@"uncheck.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)selectbtns:(id)sender {
   for (int i=0; i<[self.lblArray count]; i++)
 {

[totalcheckmarkArray replaceObjectAtIndex:i withObject:@"YES"];

 }

[self.tblvie reloadData];


 }

取消全选复选框

-(IBAction)deselectall_btnclick:(id)sender
{
  for (int i=0; i<[self.lblArray count]; i++)
 {

[totalcheckmarkArray replaceObjectAtIndex:i withObject:@"NO"];

 }

[self.tblvie reloadData];

}

关于ios - 如何在单击按钮时选择所有 uitableview 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27242180/

相关文章:

iphone - MKStoreKit 编译错误

ios - 如何在 iOS Swift 中将图像从框架发送到示例项目?

ios - 当我选择单元格时,UIImageView.image 在我的 UITableViewCell 中为 nil,即使我知道它应该有一个值

ios - UITableViewCell 在出队时重绘

ios - 我怎么知道是否选择了 UITableView 的单元格?

ios - UITableView 的 didSelectRowAtIndexPath 方法未被调用

iOS/Swift - UITableView reloadData 未被调用

ios - 我怎样才能在 Swift 5.1 (Xcode 11 Playground) 中为可选项实现泛型和非泛型函数签名的重载?

ios - Alamofire 网络调用未在后台线程中运行

ios - 如何从后端服务器拉取 IOS 应用程序中的初始化配置和更新代码?