2013-12-10 2 views
0

최근에 누군가와 비슷한 문제가 있었는지 궁금합니다.UITableViewCell dequeueReusableCellWithIdentifier : forIndexPath : issue

내 문제에 대해 자세히 설명해 드리겠습니다. 나는 IB에서 사용자 정의 UITableViewCell 세트를 설계 한 UITableViewController를 가지고있다. 각 UITableViewCell에는 UIButton, UITextField 및 UILabel 등과 같은 다른 요소가 있습니다. 분명히 각 UITableViewCell에는 다른 식별자가 있습니다.

일단 모든 내 UITableViewCells가 정의되면 다음 단계는 tableView : cellForRowAtIndexPath :에서 셀을 인스턴스화하는 것입니다. 이 메소드에서 수행하는 작업은 섹션과 행에 따라 달라지며 dequeueReusableCellWithIdentifier : forIndexPath :에 의해 다른 UITableViewCell을 인스턴스화합니다.

모든 것이 완벽하게 올바르게 작동하고 테이블에서 올바르게 생성 된 것처럼 보이지만 아래로 스크롤하면 문제가 발생합니다. 맨 위에는 클릭 할 때 수행 할 구체적 작업으로 지정한 UIBableViewCell이 있습니다. 아래로 스크롤하면 같은 형식의 UITableViewCell이 두 개 있습니다 (다른 동작이 지정된 UIButton 내부).

문제는 맨 아래에서 첫 번째 버튼을 클릭 할 때,이 버튼은 맨 위의 UIButton에 정의한 첫 번째 동작과 자체 동작을 수행한다는 것입니다.

내가 제대로 자신을 설명 것을 희망 .... uitableviewdelegate 그들을 새로운 세포를 생성하거나 재사용 할 때, 대신 지정된 indexPath의 다른 indexPath에서 기능을 둥지 것을

을 보인다.

미리 감사드립니다.

[EDIT]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
UITableViewCell *cell = [[UITableViewCell alloc] init]; 

NSLog(@"indexpath value is: %@", indexPath); 

if (indexPath.section == 0) 
    cell = [self buildSection_0:tableView getIndexPath:indexPath]; 
else if (indexPath.section == 1) 
    cell = [self buildSection_1:tableView getIndexPath:indexPath]; 
else if (indexPath.section == 2) 
    cell = [self buildSection_2:tableView getIndexPath:indexPath]; 
else if (indexPath.section == 3) 
    cell = [self buildSection_3:tableView getIndexPath:indexPath]; 
else if (indexPath.section == 4) 
    cell = [self buildSection_4:tableView getIndexPath:indexPath]; 
return cell; 
} 

-(UITableViewCell *)buildSection_0:(UITableView *)tableView getIndexPath:(NSIndexPath *)indexPath{ 
NSString *identifier; 
UITableViewCell *cell; 

if (indexPath.row == 0){ 
    identifier = @"headerCell"; 
    cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 
} 
else if (indexPath.row == 1){ 
    identifier = @"buttonCell"; 
    cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 
    UIButton *button = (UIButton *)[cell viewWithTag:3001]; 
    [button setTitle:@"Scan" forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(scan) forControlEvents:UIControlEventTouchUpInside]; 
} 

return cell; 
} 

-(UITableViewCell *)buildSection_3:(UITableView *)tableView getIndexPath:(NSIndexPath *)indexPath{ 
NSString *identifier; 
UITableViewCell *cell; 

if (indexPath.row == [[job jobLocations] count]) { // Adding button insert more Locations 
    identifier = @"buttonCell"; 
    cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 
    UIButton *button = (UIButton *)[cell viewWithTag:3001]; 
    [button setTitle:@"Add Location" forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(newLocation) forControlEvents:UIControlEventTouchUpInside]; 
} 
else{ // Showing different Locations 
    identifier = @"locationCell"; 
    cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 
    UILabel *label = (UILabel *)[cell viewWithTag:3007]; 
    UITextField *textField = (UITextField *)[cell viewWithTag:3008]; 

    [label setText:[NSString stringWithFormat:@"Location #%ld", (long)indexPath.row + 1]]; 
    [textField setText:[[[job jobLocations] objectAtIndex:indexPath.row] locationType]]; 
} 

return cell; 
} 
+0

cellForRowAtIndexPath의 게시물 코드 – Ilario

답변

1

dequeueReusableCellWithIdentifier:forIndexPath은 더 이상 보이지 않는 셀을 다시 사용하기 때문에 셀에 prepareForReuse을 구현하거나 큐에서 제거한 후에 재설정 할 수 있습니다. 당신이 당신의 코드를 추가보고

[편집]

, 당신은 이전 대상을 제거해야합니다/셀이 재사용되는 경우 여전히있을 것이다 기존의 하나로서, 새로운 사람을 추가하기 전에 버튼에서의 행동 :

[button removeTarget:self action:NULL forControlEvents:UIControlEventTouchUpInside]; 
[button addTarget:... 
+0

완벽하게 조언합니다. 고맙습니다. 그러나이 동작이 다른 셀이나 요소에 발생하는 경우, 값을 어딘가에 저장하고 다른 셀이나 요소를 그릴 때마다이 정보를 가져 오는 것이 좋습니다. 내가 맞습니까? – rubrin

0

indexPath.row에 대응하는 버튼에 태그를 추가한다. 그런 다음 버튼 작업 메서드에서 (UIButton *)sender의 태그를보고 클릭 한 버튼을 확인할 수 있습니다.

관련 문제