최근에 누군가와 비슷한 문제가 있었는지 궁금합니다.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;
}
cellForRowAtIndexPath의 게시물 코드 – Ilario