![](/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.