![](/img/trans.png)
[英]Add subview to UITableViewCell: Added several times when scrolling
[英]UISwitch added to UITableViewCell multiple times when scrolling UITableView - Swift
我正在快速使用UITableView制作一个事件菜单,其中大部分是通过编程完成的,对于该事件菜单中的某些选项,我希望从用户那里获得一个布尔值(或是/否)-因此切换会完美无缺。 我将基于单元格标签文本和部分通过cell.accessoryView
添加开关-因为我只希望在2个特定行上进行开关。 问题是添加了开关,然后向下滚动并返回到原始开关,现在我没有指定的行上还有第二个附加开关,然后向下滚动到底部并且是相同的东西那里。 首先,我想知道是否有更好的方法将开关添加到特定行(使用代码以编程方式创建单元格),并且为什么会发生这种情况? 以下代码片段:
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
}
}
设置accessoryView
到零,如果条件不匹配:
if(rowTitle == "All Day") {
cell.accessoryView = allDaySwitch
}else{
cell.accessoryView = nil
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.