UISwitch added to UITableViewCell multiple times when scrolling UITableView - Swift

时光怂恿深爱的人放手 提交于 2019-12-08 08:17:08

问题


I'm making a event-menu with UITableView in swift, the majority of it is done programatically, and for some of the options in the event-menu I would like to receive a boolean value (or yes/no) from the user - so a switch would work perfect. I am adding the switch via cell.accessoryViewbased on the cell label text and section - as I only want the switch on 2 specific rows. The problem is that the switch gets added, then when scrolling down, and back up to the original switch, there is now a second additional switch on a row that I did not specify, then I scroll back down to the bottom and the same thing there. Firstly, I would like to know if there is a better way to add the switches to specific rows (using code as the cells are created programmatically) and and also, why is this happening? Some snippets of the code below:

class PrivateNewClientController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var menuTable: UITableView!

let mainOptions = ["Client Name", "Invoicing Email", "Invoicing Address"]
let dateOptions = ["All Day", "Starts", "Ends", "Time Zone"]
let alertOptions = ["How Many Classes", "Shown on Calendar As", "Alert by Email", "Alert by Text"]
let billingOptions = ["Tax Included", "Billing Options"]
let textCellIdentifier = "TextCell"
let NumberOfSections = 4;

//For sections to have different amount of rows
let numberOfRowsAtSection: [Int] = [3, 4, 4, 2]


override func viewDidLoad() {
    super.viewDidLoad()

    //Set the table views delegate and data source to be this class itself
    menuTable.delegate = self
    menuTable.dataSource = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return NumberOfSections
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var rows: Int = 0

    if section < numberOfRowsAtSection.count {
        rows = numberOfRowsAtSection[section]
    }
    return rows
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let spacer: UILabel = UILabel()
    spacer.backgroundColor = UIColor(red: CGFloat(240/255.0), green: CGFloat(240/255.0), blue: CGFloat(244/255.0), alpha: CGFloat(1.0))

    return spacer
}

/*Adding UISwitches here*/

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell
    let row = indexPath.row

    //Configure the switches for swipe options
    let allDaySwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0));
    allDaySwitch.on = false

    let gstSwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0));
    gstSwitch.on = false


    //Assigns row labels based on sections
    if(indexPath.section == 0) {
        let rowTitle = mainOptions[row]

        //Add swipe switches
        if(rowTitle == "All Day") {
            cell.accessoryView = allDaySwitch
        }

        cell.textLabel?.text = rowTitle

    } else if(indexPath.section == 1) {

        let rowTitle = dateOptions[row]
        cell.textLabel?.text = rowTitle

    } else if(indexPath.section == 2) {

        let rowTitle = alertOptions[row]
        cell.textLabel?.text = rowTitle;

    } else {

        let rowTitle = billingOptions[row]

        //Add swipe switches
        if(rowTitle == "Tax Included") {
            cell.accessoryView = gstSwitch
        }

        cell.textLabel?.text = rowTitle
    }

    return cell
}



}

回答1:


Set accessoryView to nil if the condition dont match:

if(rowTitle == "All Day") {
    cell.accessoryView = allDaySwitch
}else{
    cell.accessoryView = nil
}


来源:https://stackoverflow.com/questions/34971329/uiswitch-added-to-uitableviewcell-multiple-times-when-scrolling-uitableview-sw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!