2013-02-26 2 views
0

내 코드에 간단한 중대한 문제가 있습니다.UITableviewcell의 UIButton 숨기기

내 Tableview에서 내 편집 단추 (네비게이션 바에 있음)를 눌렀을 때 UITableview의 편집 방법이 필요합니다. 내 셀에있는 단추와 레이블을 숨기려고합니다.

코드 :

나는이처럼 내 버튼을 추가하고 ..

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *currentCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    currentCell = nil; 
    if (currentCell==nil) 
    { 
     currentCell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    BtnEmail=[UIButton buttonWithType:UIButtonTypeCustom]; 
    //BtnEmail = (UIButton *)[currentCell viewWithTag: 3]; 
    BtnEmail.frame=CGRectMake(265, 17, 25, 25); 
    BtnEmail.tag=indexPath.row; 
    //[BtnEmail setTitle:@"E-mail" forState:UIControlStateNormal]; 
    [BtnEmail setBackgroundImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal]; 
    [BtnEmail addTarget:self action:@selector(BtnEmail:) forControlEvents:UIControlEventTouchUpInside]; 
    [currentCell.contentView addSubview:BtnEmail]; 
    [currentCell.contentView bringSubviewToFront:BtnEmail]; 



    return currentCell; 
} 

내 편집 버튼이

편집 버튼과 같이 선언한다 :

self.navigationItem.rightBarButtonItem = self.editButtonItem; 

및 편집에 내 방법을 클릭하십시오를 클릭하십시오.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 
{ 
    [super setEditing:editing animated:animated]; 
    [listTableView setEditing:editing animated:animated]; 

    if(editing) 
    { 
     self.navigationItem.leftBarButtonItem.enabled = NO; 
     //BtnEmail.frame=CGRectMake(355, 17, 25, 25); 
     BtnEmail.hidden = TRUE; 
    } 
    else 
    { 
     self.navigationItem.leftBarButtonItem.enabled = YES; 
     //BtnEmail.frame=CGRectMake(265, 17, 25, 25); 
     BtnEmail.hidden = FALSE; 
    } 

    [super setEditing:editing animated:animated]; 
} 

이 경우에는 마지막 셀 Button과 Lable 모두를 숨길 것입니다. 내 셀의 모든 UIButton을 숨겨야합니다.

나는 테이블에 3 셀을 가지고 경우 다음은 마지막으로 버튼을 숨길처럼 만 ..

감사합니다. 이 경우 마지막 할당 된 버튼을 cellForRowAtIndexPath 방법이다

BtnEmail.hidden = FALSE; 

를 선언하기 때문이다

+0

이 경우 BtnEmail에 대한 마지막으로 생성 된 참조가 표시되기 때문입니다. – Exploring

답변

3

당신은 cellForRow에 단순히 조건 확인을 할 수 있으며, 편집이 변경 될 때 테이블 뷰를 다시로드 :이 같은 것을보십시오.

하지만 맞춤식 셀을 사용하여 버튼을 쉽게 만들 수 있습니다. 여기서 버튼을 몇 번이나 추가하고 있습니다. 그렇지 않으면 버튼 생성 코드를 if(cell == nil) 블록으로 이동하십시오.

+0

Anil 고마워요 .... 지금은 잘 작동합니다 ... !! – Vivek2012

0

.

if(editing) 
     { 
      self.navigationItem.leftBarButtonItem.enabled = NO; 
      //BtnEmail.frame=CGRectMake(355, 17, 25, 25); 
      For(int i=0; i< [tableArray count]; i++) 
     { 
      UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] ; 
      UIButton *btn=(UIButton *)[cell viewWithTag:i]; 
      btn.hidden = TRUE; 
     } 
     } 
    else 
    { 
     self.navigationItem.leftBarButtonItem.enabled = YES; 
      //BtnEmail.frame=CGRectMake(265, 17, 25, 25); 
     For(int i=0; i< [tableArray count]; i++) 
     { 
      UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] ; 
      UIButton *btn=(UIButton *)[cell viewWithTag:i]; 
      btn.hidden = FALSE; 
     } 
     } 
+0

어디서이 코드를 작성해야합니까? – Vivek2012

+0

답변을 편집했습니다. 확인하십시오 –

+0

상단이 잘 작동합니다 .. 도움을 제공해 주셔서 감사합니다. @iPhone 개발자. – Vivek2012

관련 문제