簡體   English   中英

如何在單擊按鈕時選擇所有uitableview單元格

[英]How to select all uitableview cells on button click

我正在制作一個應用程序,其中我在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];


   }
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;
 }

selectAll復選框

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

}

我可以想到兩種可能的解決方案。 如果以下行在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移除。 下面的另一個答案顯示了如何將所有圖像添加到陣列的示例。

你可以做 。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM