【问题标题】:UISwitch added to UITableViewCell multiple times when scrolling UITableView - Swift滚动 UITableView 时 UISwitch 多次添加到 UITableViewCell - Swift
【发布时间】:2016-04-30 12:09:46
【问题描述】:

我正在用 UITableView 快速制作一个事件菜单,其中大部分是通过编程完成的,对于事件菜单中的某些选项,我想从那里接收一个布尔值(或是/否)用户 - 所以一个开关会很完美。我基于单元格标签文本和部分通过cell.accessoryView 添加开关 - 因为我只希望在 2 个特定行上进行开关。问题是添加了开关,然后向下滚动并返回到原始开关时,现在在我未指定的行上有第二个附加开关,然后我向下滚动到底部和同样的事情那里。首先,我想知道是否有更好的方法将开关添加到特定行(使用代码,因为单元格是通过编程方式创建的),以及为什么会发生这种情况?以下代码的一些sn-ps:

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
}



}

【问题讨论】:

    标签: ios swift uitableview uiswitch


    【解决方案1】:

    如果条件不匹配,则将 accessoryView 设置为 nil:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 2017-01-06
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多