Reputation: 1451
I have a static table view with different sections. Here you can see one of the sections:
Users can tap the cells in these sections to check them, but I want them not to be able to tap two cells from the same section, for example, if they have tapped the cell named "Name A-Z", then they tap the one named "Name Z-A", the first one gets unchecked, and the second one gets checked. To do so, I think, I should somehow check whether two cells are from the same section or not, but I don't know how to implement that. I'm using
tableView(_ didSelectRowAt:)
method for selecting the cells, but I don't know how to access cells' sections from there. Maybe I should use another method? Any ideas how to implement this?
Upvotes: 2
Views: 1569
Reputation: 576
Okay so the code below lets you do a specific action whenever a cell is selected. The code recognizes what section it is in automatically as well.
Currently the table updates the label to say selected and automatically sets the other two to N/A. You can replace this with your own code to hide/show a checkmark or something.
Here is a video of what the following code does as well.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case 0:
switch indexPath.row {
case 0:
sectionOneCellOne.textLabel?.text = "SELECTED"
sectionOneCellTwo.textLabel?.text = "N/A"
sectionOneCellThree.textLabel?.text = "N/A"
case 1:
sectionOneCellOne.textLabel?.text = "N/A"
sectionOneCellTwo.textLabel?.text = "SELECTED"
sectionOneCellThree.textLabel?.text = "N/A"
case 2:
sectionOneCellOne.textLabel?.text = "N/A"
sectionOneCellTwo.textLabel?.text = "N/A"
sectionOneCellThree.textLabel?.text = "SELECTED"
default:
break
}
case 1:
switch indexPath.row {
case 0:
sectionTwoCellOne.textLabel?.text = "SELECTED"
sectionTwoCellTwo.textLabel?.text = "N/A"
sectionTwoCellThree.textLabel?.text = "N/A"
case 1:
sectionTwoCellOne.textLabel?.text = "N/A"
sectionTwoCellTwo.textLabel?.text = "SELECTED"
sectionTwoCellThree.textLabel?.text = "N/A"
case 2:
sectionTwoCellOne.textLabel?.text = "N/A"
sectionTwoCellTwo.textLabel?.text = "N/A"
sectionTwoCellThree.textLabel?.text = "SELECTED"
default:
break
}
default:
break
}
}
The sectionOneCellOne
variables and such are just individual cells defined in the View Controller.
Upvotes: 2
Reputation: 186
You could do exactly what lufritz said.
Create a variable in your viewController to have your selected index
var selectedIndex = -1
you can set the value with -1, because 0 is the first cell of the table.
i dont know how you want to make the select effect, in this example i just change the background color, created two functions for select and deselect the cells:
func selectCell(cell:UITableViewCell){
cell.backgroundColor = .green
}
func deselectCell(cell:UITableViewCell){
cell.backgroundColor = .white
}
After that in your cellForRowAt indexPath:
method you can do something like:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell"){
cell.textLabel?.text = "Example \(indexPath.row)"
deselectCell(cell: cell)
if selectedIndex == indexPath.row{
selectCell(cell: cell)
}
return cell
}
return UITableViewCell.init()
}
if the selectedIndex
is the current indexPath.row
you use the selectCell method, but first you have to use the deselect method, for deselect the last one.
the last thing is set the selectedIndex
variable, like you thought, put it in didSelectRowAt indexPath
method:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedIndex = indexPath.row
tableView.reloadData()
}
Upvotes: 0
Reputation: 1568
In func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
you can get the section using indexPath.section
. Store the indexPaths of selections in an array and check for and replace the indexPath with the same section when one is encountered inside func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
.
Like this:
var selectedIndexPaths:[IndexPath] = []
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if let sameSectionPathIndex = selectedIndexPaths.index(where:{$0.section == indexPath.section}){
if let previousSelectedCell = tableView.cellForRow(at:selectedIndexPaths[sameSectionPathIndex]){
//Do unchecking and anything else with the previously selected cell
}
selectedIndexPaths.remove(at: sameSectionPathIndex)
selectedIndexPaths.insert(indexPath, at: sameSectionPathIndex)
} else {
selectedIndexPaths.append(indexPath)
}
if let toBeSelectedCell = tableView.cellForRow(at: indexPath){
// Do checking and anything else you want with the newly selected cell
}
}
Upvotes: 1