2012-05-27 3 views
2

4 개의 섹션이있는 기본 UITableView가 있습니다. Switch 문을 사용하여 각 섹션의 내용을 제어합니다.IOS : 셀을 UITableView에서 잘못 재사용 중지

첫 번째 섹션의 행에 나타나야하지만 네 번째 섹션에는 나타나지 않아야하는 버튼을 프로그래밍 방식으로 만듭니다. 그러나 버튼은 네 번째 섹션의 행에 무작위로 나타납니다.

셀 재사용 때문입니다. 그러나 Switch 문을 사용하여 각 섹션의 행을 만들 때 어떻게되는지 알 수 없습니다. 어떤 아이디어라도 감사합니다.

내가 그렇게 구성된 사용자 정의 셀을 사용하고 있습니다 :`

static NSString *CustomCellIdentifier = @"DashboardCell"; 

DashboardCell *cell = (DashboardCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 

if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DashboardCell" 
                   owner:self options:nil]; 
    for (id oneObject in nib) if ([oneObject isKindOfClass:[DashboardCell class]]) 
     cell = (DashboardCell *)oneObject; 
} 

// Configure the cell.` 

이 버튼을 작성하는 코드 것은`

 button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(200, 11, 50, 50);   
     UIImage *iConnect = [UIImage imageNamed:@"connect.png"]; 
     [button setImage:iConnect forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell addSubview:button];` 

답변

11

은 당신이 다른 재사용 식별자를 필요로하는 방법 cellForRowAtIndexPath 당신의 구성 부분에 해제 각 유형의 콘텐츠. 따라서 여기에는 UIButton의 셀과 그렇지 않은 셀의 두 가지 유형의 컨텐츠가 있습니다.

tableView:cellForRowAtIndexPath: 메서드의 indexPath 메서드를 사용하여 @ "CellWithButton"또는 @ "CellWithoutButton"중 하나의 재사용 식별자를 선택합니다.

코드에서 실제로 일어나고있는 것은 모든 셀에 동일한 재사용 식별자가 주어 지므로 모든 코드가 동일한 개체 풀에 저장된다는 것입니다. 이는 [tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];을 사용하여이 하나의 풀 (어떤 셀에도 UIButton이없는 셀과 셀을 포함 할 수 있음)에서 셀을 검색한다는 것을 의미합니다. 따라서 dequeue 메소드는 이미 UIButton이 추가 된 셀을 임의로 반환 할 수 있습니다. 두 개의 재사용 식별자를 사용하는 경우 UITableView은 두 개의 객체 풀을 설정하고 각각에서 적절한 셀을 입금하고 검색합니다.

또는 dequeue 메소드를 사용하여 하나의 재사용 풀을 검색 할 때마다 하나의 재사용 풀을 사용하고 UIButton 셀을 확인할 수 있습니다.

+0

감사합니다. Barjavel. 두 가지 세포 유형을 만들었습니다. 매력처럼 작동합니다. – Jeremy

1

당신은 섹션에 따라 서로 다른 두 가지 세포 식별자를 사용할 수 있습니다. 그렇지 않으면 dequeueReusableCellWithIdentifier:에서 반환 된 셀에 버튼이 있는지 확인하고 필요한 경우 기존 버튼을 추가하거나 제거해야합니다.

+0

이전 버전의 버튼 위에 새로운 컨트롤을 추가 할 가능성이 높기 때문에 추가하지 않으면 작동한다고 생각하겠습니다. 작동하는 것처럼 보이는 버튼을 그리지 않는 경우가 있습니다. – madmik3

+0

@ madmik3 - 버튼 생성 코드가 어떤 조건에서 실행되는지에 대한 질문에서 분명하지 않지만 아마 맞을 것입니다. 어쩌면 직접적으로 또는 어쩌면 들여 쓰기가 의미를 가질 수도 있습니다. [으 시구] : –

7

DashboardCell에서 @property (nonatomic, assign) BOOL buttonEnabled 속성을 만듭니다. 그런 다음 awakeFromNib에서 항상 버튼을 만들고 button.hidden = YES을 설정하십시오.

- (void)setButtonEnabled:(BOOL)enabled { 
    buttonEnabled = enabled; 
    button.hidden = !enabled; 
} 

그리고 마지막으로는 prepareForReuse

- (void)prepareForReuse { 
    [super prepareForReuse]; 
    self.buttonEnabled = NO; 
} 

를 오버라이드 (override) 재산의 세터를 무시하고 이제 enbale 수/

+0

고마워요 @iTukker !! – Lito

+0

이것은 받아 들여진 대답이어야하고, 사용하기 아주 쉽습니다. - (void) prepareForReuse –

0

이러한 셀을 재사용하고 각 셀의 버튼을 숨기거나 표시하는 간단한 논리가 있다면 먼저 권장할만한 것은 인터페이스 작성기에서 버튼을 만들고 UITableViewDelegate에 연결하는 것입니다. .

그때 당신은 언제든지 실행할 수있는 셀에 대한 설정 방법을 만드는 것, 그것없이 여러 번 그것을 깨는 :

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

    DashboardCell *cell = (DashboardCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 

    if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DashboardCell" 
                   owner:self options:nil]; 
     for (id oneObject in nib) if ([oneObject isKindOfClass:[DashboardCell class]]) 
      cell = (DashboardCell *)oneObject; 
    } 

    // Configure the cell. 
    [cell setupWithSomeConfigOptions:someConfigOptions] 

    return cell; 
} 

을 그리고 당신의 세포 하위 클래스에서이 방법 -(void)setupWithSomeOptions:(SomeOptions)someOptions있을 것입니다,

-(void)setupWithSomeOptions:(SomeOptions)someOptions 
{ 
    // some setup code 

    self.myButtonOutlet.hidden = someOptions.somePropertyToCheckIfButtonShouldBeHidden; 

    // some more setup code 
} 
관련 문제