【问题标题】:How to select all uitableview cells on button click如何在单击按钮时选择所有 uitableview 单元格
【发布时间】:2015-01-30 06:32:06
【问题描述】:

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

- (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];


}

这是我的按钮,我在其中单击,然后 tableview 的所有单元格都将被选中,所有单元格的图像都会更改,但它不起作用

- (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];


   }

【问题讨论】:

  • ru 在此 [cell.buttonCheckbox setImage:[UIImage imageNamed:@"check_box_ok.png"] forState:UIControlStateNormal]; 行后尝试添加 [yourtableview reloaddata]
  • 是的尝试过,但是“无法加载从包中带有标识符“com.xxx.whyq.checkboxwithTableview”的笔尖引用的“”图像”
  • 清理并再次运行一次
  • 好你最终需要的结果
  • 我想在点击按钮时选择所有单元格。

标签: ios uitableview


【解决方案1】:
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];

}

【讨论】:

  • 一个异常终止应用程序由于未捕获的异常'NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:空数组的索引0超出范围'
  • 我在 viewdidLoad 中更改了这一行 [totalcheckmarkArray replaceObjectAtIndex:i withObject:@"NO"]; 试试这个
  • 同样的异常即将到来
  • 等我脸皮告诉你
  • 什么是 checkimageArray ?
【解决方案2】:

我能想到两种可能的解决方案。如果以下行在 cellForRowAtIndexPath 中有效:

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

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

}

然后只需将所有图像添加到self.checkimageAray,然后通过[self.tblvie reloadData]重新加载表格。

如果出于某种原因您不想重新加载表格,您可以执行以下操作,这将遍历表格中的所有单元格并执行对每个单元格的更改:

for (int section = 0; section < [self.tblvie numberOfSections]; section++) {
    for (int row = 0; row < [self.tblvie numberOfRowsInSection:section]; row++) {
        NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
        TablecellTableViewCell *cell= [self.tblvie  cellForRowAtIndexPath:cellPath];
        [cell.buttonCheckbox setImage:[UIImage imageNamed:@"check_box_ok.png"]
                     forState:UIControlStateNormal];
    }
}

当我希望对动画进行更改时,我倾向于遍历每个单元格。如果说你想为复选框的检查设置动画,最简单的方法是将动画块放置在单元格的迭代中。

如果您想取消选择一个单元格,您可以执行以下操作:

for (int section = 0; section < [self.tblvie numberOfSections]; section++) {
    for (int row = 0; row < [self.tblvie numberOfRowsInSection:section]; row++) {
        NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
        TablecellTableViewCell *cell= [self.tblvie  cellForRowAtIndexPath:cellPath];
        [cell.buttonCheckbox setImage:[UIImage imageNamed:@"uncheck.png"]
                         forState:UIControlStateNormal];
    }
}

请注意,这不会改变您的checkimageArray。当单元格显示为选中状态时,所有图像都不在您的checkimageArray 中,并且与我取消选择单元格的解决方案相比,图像也不会从 checkimageArray 中删除。

我建议将self.lblArray 中的所有对象添加到checkimageArray,您可以在其中放置代码以设置单元格突出显示,并在您取消选择所有对象时从checkimageArray 中删除所有对象。如何将所有图像添加到数组中的示例显示在下面的另一个答案中。

【讨论】:

  • 好的,这是用于选择单元格的。现在我该怎么做才能取消选择它们?解决方案 2
  • 我用取消选择单元格的解决方案更新了我的答案。就像我在更新的答案中所说的那样,我强调这对所选图像的数组没有任何作用。这意味着您只是将图像显示为选定的,而不是在幕后实际选择它们。当您希望对所选图像执行某些操作时,这会导致问题。就像我建议的那样,处理数组以及单元格的外观非常简单。如果需要,我很乐意提供帮助。
【解决方案3】:

你可以做到。

- (IBAction)selectbtns:(id)sender 
{
     self.checkimageArray = [NSArray arrayWithArray:self.lblArray];
     [self.tableView reloadData];
}

【讨论】:

  • 那个警告是什么?
猜你喜欢
  • 1970-01-01
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多