Мне интересно, есть ли у кого-то аналогичная проблема, которую я экспериментировал в последнее время.UITableViewCell dequeueReusableCellWithIdentifier: forIndexPath: issue
Позвольте мне подробнее описать вашу проблему. У меня есть UITableViewController, где я разработал набор пользовательских UITableViewCell в IB. Каждый UITableViewCell имеет разные элементы, такие как UIButton, UITextField и UILabel и т. Д. Очевидно, что каждый UITableViewCell имеет другой идентификатор.
После определения всех моих UITableViewCells следующий шаг - создать экземпляр ячейки на tableView: cellForRowAtIndexPath :. То, что я делаю в этом методе, зависит от раздела и строки, я создаю экземпляр различных UITableViewCells с помощью dequeueReusableCellWithIdentifier: forIndexPath :.
Кажется, что все работает отлично и правильно создано на столе, но проблема возникает при прокрутке вниз. На самом верху, у меня есть UITableViewCell с UIButton, который я указал с конкретным действием, которое нужно выполнить при нажатии. При прокрутке вниз есть несколько UITableViewCells с одинаковым форматом (внутри 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