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.accessoryView
based 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
}
}
Set accessoryView
to nil if the condition dont match:
if(rowTitle == "All Day") {
cell.accessoryView = allDaySwitch
}else{
cell.accessoryView = nil
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.