2014-12-02 4 views
2

uitableview cell.i에있는 확인란의 형태로 버튼을 사용하는 응용 프로그램을 만들고 있습니다. 해당 확인란의 이미지를 변경하고 상태를 선택 취소하고 있습니다. 이제 내가 다른 단추를 클릭하면 표보기의 모든 셀이 선택되고 셀의 모든 체크 상자의 이미지가 변경됩니다. 방법은 아래 할 수있는 것은 내 샘플 코드버튼 클릭시 모든 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

RU이 ** [cell.buttonCheckbox의 setImage 후 시도 : [있는 UIImage imageNamed : @ "check_box_ok.png" ] forState : UIControlStateNormal]; ** line add [yourtableview reloaddata] –

+0

예, ""com.xxx.whyq라는 식별자가있는 번들의 참조 된 이미지를로드 할 수 없습니다.checkboxwithTableview " " –

+0

다시 한 번 다시 실행하십시오. –

답변

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

는 selectAll 확인란

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

} 
+0

예외 캐치되지 않은 예외 'NSRangeException'으로 인해 응용 프로그램을 종료하는 이유 : '*** - [NSArrayM objectAtIndex :] : 빈 배열의 범위를 벗어나는 색인 0 –

+0

viewdidLoad **에서이 행을 변경했습니다. [totalcheckmarkArray replaceObjectAtIndex : i withObject : @ "NO"] ** 시도해보십시오 –

+0

동일한 예외가 발생합니다 –

0

할 수 있습니다.

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

경고가 올 것입니다 ... –

+0

그 경고는 무엇입니까? – Dev

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.lblArray에서 checkimageArray까지 모든 개체를 추가하여 강조 표시된 셀을 설정하고 모든 개체를 checkimageArray에서 모두 제거 할 것을 권장합니다. 모든 이미지를 배열에 추가하는 방법의 예가 다른 답변에 표시됩니다.

+0

이것은 셀 선택을위한 것입니다 .. 이제는 그들을 모두 선택 취소 할 수 있습니까 ??? 솔루션 2에 대한 –

+0

셀의 선택을 취소하기위한 솔루션으로 내 대답이 업데이트되었습니다. 업데이트 된 답변에서 언급했듯이 선택한 이미지 배열에 아무런 변화가 없다고 강조합니다. 즉, 선택한 이미지를 표시하는 것 뿐이지 만 실제로 장면을 선택하지는 않습니다. 이렇게하면 선택한 이미지로 무언가를하고 싶을 때 문제가 발생할 수 있습니다. 필자가 제안한 것처럼 배열과 셀 모양을 처리하는 것은 매우 간단합니다. 필요한 경우 도와 드리겠습니다. – ferris

관련 문제