2012-01-08 2 views
0

테이블보기가 있습니다. 그리고 메신저는 각 셀에 두 개의 버튼을 추가 : 테이블이 "편집"모드에있을 때프로그래밍 방식으로 UIButton을 추가 할 때 UITableView 버그가 발생했습니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     newBtn = [[UIButton alloc]init]; 
     newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [newBtn setFrame:CGRectMake(250,10,25,55)]; 
     [newBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside]; 
     [newBtn setTitle:@"+" forState:UIControlStateNormal]; 
     [newBtn setEnabled:YES]; 
     newBtn.hidden = YES; 
     [cell addSubview:newBtn]; 

     subBtn = [[UIButton alloc]init]; 
     subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [subBtn setFrame:CGRectMake(280,10,25,55)]; 
     [subBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside]; 
     [subBtn setTitle:@"-" forState:UIControlStateNormal]; 
     [subBtn setEnabled:YES]; 
     subBtn.hidden = YES; 
     [cell addSubview:subBtn]; 

    } 
return cell; 
} 

그리고 난 처음에 숨겨진 버튼을 갖고 싶어, 그럼,이 버튼을 표시 할. 테이블이 "편집"모드를 벗어나면 버튼이 사라집니다.

이렇게하려면 셀 단추 중 하나를 가져올 수 있습니다.

- (IBAction)editButton:(id)sender 
{ 
    if (self.editing) 
    { 
     [self setEditing:NO animated:YES]; 
     [self.myTableView setEditing:NO animated:YES]; 
     EditButton.title = @"Edit"; 
     subBtn.hidden = YES; 
     newBtn.hidden = YES; 
    } 
    else 
    { 
     [self setEditing:YES animated:YES]; 
     [self.myTableView setEditing:YES animated:YES]; 
     EditButton.title = @"Done"; 
     subBtn.hidden = NO; 
     newBtn.hidden = NO; 
    } 
} 

그러나 문제는 다음과 같습니다. 이렇게하면 가장 마지막 셀만 단추를 가져옵니다. 그들은 원할 때 정확히 나타나고 사라지지만, 마지막 세포 만 나타납니다! 다른 어떤 세포도 버튼을 얻지 못합니다. 누군가 나를 도와주세요! 정말 고마워!

답변

1

이 방법은 마지막 셀의 단추를 가리키는 subBtnnewBtn입니다. 더 좋은 방법은 UITableViewCell을 서브 클래스 화하고 버튼 인스턴스 변수를 만드는 것입니다. 그런 다음 - (void)setEditing:(BOOL)editing animated:(BOOL)animated을 덮어 쓰고 버튼을 숨기거나 표시하십시오.

+0

좀 더 설명해 주시겠습니까? Im Noob! "서브 콜 (subcall)"이란 무엇을 의미합니까? 너무 많은 번거 로움이 없다면, 그것을 공부하고 프로그램에서 사용할 수있는 몇 가지 샘플 코드를 제공해 주시겠습니까? 도와 주셔서 감사합니다! – iProRage

+0

또한 내 버튼이 마지막 셀의 버튼을 가리키고 있습니까? 어떤 코드가 그것을하는지 모르겠다! 다시 한 번 감사드립니다! : D – iProRage

+0

나는 '하위 클래스'를 의미했다. 당신이 할 때마다보십시오 "subBtn = [[UIButton alloc] init];" 귀하의 포인터가 분명히 재정의됩니다, 그래서 결국 그것은 마지막 셀의 버튼을 가리 킵니다. 이것을 더 잘 설명하는 법을 모르십시오. –

1

어떻게 '편집 버튼'방법을 실행하고 있습니까? didSelectRowAtIndexPath를 사용하는 경우 선택한 행만 단추를 표시합니다. 보이는 셀에 대해 indexpath.row를 반복하면서 각 행을 숨길 수 있습니다.

+0

그냥 탐색 막대의 단추에서이 메서드를 호출하고 도움을 주셔서 감사합니다! – iProRage

관련 문제