Sunday, 1 December 2019

Restrict cell selection per section in UITableView swift

I want to restrict certain sections of my tableview to only allow 1 cell to be selected, as of right now all my cells can be selected regardless of the section it's in

There is a little twist however : My sections are an [array] and change dynamically depending on different variables.

My sections are themselves each, a Variable, so I can pinpoint to them programmatically like this :

var section1 = [NSDictionary(objects: [NSLocalizedString("Alcohol use less than 24 hours", comment:""), 2],

As said before however, section1 might need to be restricted while section4 might not.

Some code from the tableView :

// checkmarks when tapped

    func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) {
            if self.selectedIndexPaths.contains(indexPath) {

                cell.accessoryType = .none
                cell.backgroundColor = UIColor.clear
                self.selectedIndexPaths.remove(indexPath)

                if CrewMembersNumber == "1" {
                    if((indexPath).section == 0) {
                        self.section1score -= section1[(indexPath).row].object(forKey: "value") as! Int
                    } else if((indexPath).section == 1) {
                        self.section3score -= section3[(indexPath).row].object(forKey: "value") as! Int
                    }

                } else if CrewMembersNumber == "2" {
                    if((indexPath).section == 0) {
                        self.section1score -= section1[(indexPath).row].object(forKey: "value") as! Int
                    } else if((indexPath).section == 1) {
                        self.section2score -= section2[(indexPath).row].object(forKey: "value") as! Int
                    }
                } else if CrewMembersNumber == "3" {
                    if((indexPath).section == 0) {
                        self.section1score -= section1[(indexPath).row].object(forKey: "value") as! Int
                    } else if((indexPath).section == 1) {
                        self.section5score -= section5[(indexPath).row].object(forKey: "value") as! Int
                    }

                } else {
                    // if crewmemebernumber doest return 1-2 or 3
                    if((indexPath).section == 0) {
                        self.section1score -= section1[(indexPath).row].object(forKey: "value") as! Int
                    } else if((indexPath).section == 1) {
                        self.section4score -= section4[(indexPath).row].object(forKey: "value") as! Int
                    }

                }

            } else {
                cell.accessoryType = .checkmark
                cell.backgroundColor = UIColor (red:236/255.0, green: 236/255, blue: 236/255, alpha: 1.0)
                self.selectedIndexPaths.add(indexPath)

                if CrewMembersNumber == "1" {
                    if((indexPath).section == 0) {
                        self.section1score += section1[(indexPath).row].object(forKey: "value") as! Int
                    } else if((indexPath).section == 1) {
                        self.section3score += section3[(indexPath).row].object(forKey: "value") as! Int
                    }

                } else if CrewMembersNumber == "2" {
                    if((indexPath).section == 0) {
                        self.section1score += section1[(indexPath).row].object(forKey: "value") as! Int
                    } else if((indexPath).section == 1) {
                        self.section2score += section2[(indexPath).row].object(forKey: "value") as! Int
                    }

                } else if CrewMembersNumber == "3" {
                    if((indexPath).section == 0) {
                        self.section1score += section1[(indexPath).row].object(forKey: "value") as! Int
                    } else if((indexPath).section == 1) {
                        self.section5score += section5[(indexPath).row].object(forKey: "value") as! Int
                    }
                } else {
                    // if crewmemebernumber doest return 1-2 or 3
                    if((indexPath).section == 0) {
                        self.section1score += section1[(indexPath).row].object(forKey: "value") as! Int
                    } else if((indexPath).section == 1) {
                        self.section4score += section4[(indexPath).row].object(forKey: "value") as! Int
                    }
                }
            }
            self.updateToolbarAndLabel(self.totalScore)
            self.tableView.reloadData()
        }
    }


    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel!.text = self.textForIndexPath(indexPath);
        cell.textLabel!.font = UIFont(name:"Avenir", size:19)
        cell.textLabel!.numberOfLines = 0
        cell.selectionStyle = UITableViewCellSelectionStyle.none;

        if(self.selectedIndexPaths.contains(indexPath)) {

            cell.accessoryType = .checkmark;
            cell.backgroundColor = UIColor (red:236/255.0, green: 236/255, blue: 236/255, alpha: 1.0)
        } else {
            cell.accessoryType = .none;
            cell.backgroundColor = UIColor.clear
        }
        return cell
 }

Right now the code behaves the same in every section, and all cells are selectable no problem, I want to be able to select only 1 cell in section4 and section5

my question goes like this,

1st : how can I create a variable or something else that keeps track of which sections need to be restricted (like section4 section need to be restricted so I need a way to "flag" them)

2nd : how can I restrict a section to only allow for 1 cell to be selected (and automatically deselect the last one selected)

thanks



from Restrict cell selection per section in UITableView swift

No comments:

Post a Comment