Я думаю, вам нужно создать для этого дополнительную логику, например.
Вы можете поместить в один массив все выбранные indexPath и всегда сравнивать первый элемент (indexPath) с каждым элементом, который вы хотите добавить, indexPath содержит свойство Section, поэтому просто нужно сравнить, равен ли раздел или нет, если это не так, вы показываете оповещение, которое хотите, после этого вы отменяете выбор элемента вручную.
var selectedItems = [IndexPath]()
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let firstIndexPath = selectedItems.first{
//If the section with the first element selected and the new selection are not equals, display your message
if indexPath.section != firstIndexPath.section{
let alert = UIAlertController(title: "Warning", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: {
a in
self.selectIndexPath(indexPath: indexPath, isSelected: false)
}))
//You can put it here if you prefer not wait for the closure for realize the deselection
//self.selectIndexPath(indexPath: indexPath, isSelected: false)
self.present(alert, animated: true, completion: nil)
return
}
self.selectIndexPath(indexPath: indexPath, isSelected: true)
}else{
self.selectIndexPath(indexPath: indexPath, isSelected: true)
}
}
func selectIndexPath(indexPath: IndexPath, isSelected: Bool){
tableView.cellForRow(at: indexPath)?.accessoryType = isSelected ? .checkmark : .none
if isSelected{
selectedItems.append(indexPath)
}else{
selectedItems.removeAll(where: {$0 == indexPath})
tableView.deselectRow(at: indexPath, animated: true)
}
let section = sections[indexPath.section]
let item = section.items[indexPath.row]
// for all selected rows assuming tableView.allowsMultipleSelection = true
}