Так что я не уверен, как написать простой заголовок, который объясняет мою проблему.iOS: UITableView изменяет состояние выбора при прокрутке
У меня есть UITableView на моем экране. Пока вид таблицы меньше экрана (все ячейки подходят на экране без прокрутки), все в порядке. Однако, как только пользователь должен прокрутить экран, чтобы найти ячейку, которая находится в нижней части таблицы, некоторые ячейки случайным образом меняют свое состояние выбора. я реализовал UITableView так:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier = @"";
switch (indexPath.section)
{
case 1:
{
if (indexPath.row == 0)
{
simpleTableIdentifier = @"EventDateCell";
}
else
{
simpleTableIdentifier = @"EventDateSelectCell";
}
[...]
return cell;
}break;
case 2:
{
simpleTableIdentifier = @"SelectPlayerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
Player *player = [self.playerController getPlayerAtPosition:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", player.persistentData.name];
return cell;
}break;
default:
{
simpleTableIdentifier = @"EventNameCell";
[...]
return cell;
}break;
}
}
ли Xcode изменить indexPath.row в соответствии с клетками, которые показаны на экране?
Код для обозначения выбранной ячейки:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 1 && indexPath.row == 0)
{
[..]
}
else if (indexPath.section == 2)
{
// close date picker if it is still open
if (self.selectingEventDate == TRUE)
{
[...]
}
if(self.createdEvent != nil)
{
if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark)
{
[self tableView:tableView didDeselectRowAtIndexPath:indexPath];
}
else
{
[[tableView cellForRowAtIndexPath:indexPath] setSelected:TRUE];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryCheckmark];
[self.eventController addPlayerToEvent:self.createdEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}
}
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 1 && indexPath.row == 0)
{
[...]
}
else if (indexPath.section == 2)
{
if(self.createdEvent != nil)
{
[[tableView cellForRowAtIndexPath:indexPath] setSelected:FALSE];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryNone];
[self.eventController removePlayerFromEvent:self.createdEvent :[self.playerController getPlayerAtPosition:indexPath.row] :FALSE];
}
}
}
я узнал: Если первая ячейка помечается, есть 12 ячеек, которые отображаются правильно, а 13 проверяется.
Проверьте мой ответ это работает в моем проекте –