2014-12-02 83 views
2

我正在製作一個應用程序,我在uitableview單元格中使用複選框形式的按鈕。我正在改變該複選框上的圖像選擇和取消選擇狀態。現在我想,當我點擊另一個按鈕,然後表格視圖的所有單元格將被選中,單元格的所有複選框的圖像將改變?如何它可以在下面做的是我的示例代碼如何選擇按鈕上的所有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]]; 
     [email protected]""; 

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


    } 
+0

る嘗試:[UIImage的imageNamed:@ 「check_box_ok.png」 ] forState:UIControlStateNormal]; **添加行[yourtableview reloaddata] – 2014-12-02 05:09:46

+0

是啊,但試過,但「無法加載」標識符爲「com.xxx.whyq的包中的一個筆尖引用的圖像」。checkboxwithTableview「 」 – 2014-12-02 05:15:31

+0

清潔一次 – 2014-12-02 05:16:23

回答

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


} 

DeselectAll複選框

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

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

} 

[self.tblvie reloadData]; 

} 
這個** [cell.buttonCheckbox setImage後
+0

異常終止應用程序由於未捕獲的異常'NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:索引0超出空數組的界限' – 2014-12-02 05:39:53

+0

我改變了這一行viewdidLoad ** [totalcheckmarkArray replaceObjectAtIndex:i withObject: @「NO」]; **試試這個 – 2014-12-02 05:41:55

+0

同一異​​常即將 – 2014-12-02 05:43:27

0

你可以做。

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

警告來了... – 2014-12-02 05:26:22

+0

那是什麼警告? – Dev 2014-12-02 05:43:15

1

我可以想到兩種可能的解決方案。如果下面的行中的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.lblArraycheckimageArray您放置代碼來設置突出了細胞,當你取消他們全部來自checkimageArray刪除所有對象。如何將所有圖像添加到數組的示例顯示在下面的其他答案中。

+0

確定這是選擇單元格..現在我能做些什麼來取消選擇所有這些???爲解決2 – 2014-12-02 05:33:28

+0

我更新我的回答與不選擇的細胞的溶液。就像我在更新的答案中所述,我強調這不會對所選圖像陣列做任何事情。這意味着您只是將圖像顯示爲選定的圖像,而不是在幕後選擇它們。當您想要對所選圖像進行操作時,這會造成問題。就像我建議的那樣,處理數組以及單元格的外觀非常簡單。如果需要,我很樂意提供幫助。 – ferris 2014-12-02 07:14:15

相關問題