无法更改UITableViewCell属性

时间:2014-09-17 21:34:28

标签: uitableview swift

我正在尝试更改表格视图单元格的附件视图/附件类型,但它不起作用。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell?

    switch indexPath.row {
        case 0:
            cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "enabledCell")
            cell!.textLabel?.text = "Alarm enabled"
            var enabledSwitch = UISwitch(frame: CGRectZero)
            cell!.accessoryView = enabledSwitch
        case 1:
            cell = self.tableView.dequeueReusableCellWithIdentifier("calendarCell", forIndexPath: indexPath) as? UITableViewCell
            cell!.textLabel?.text = "Calendar"
            cell!.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        default:
            cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? UITableViewCell
            cell!.textLabel?.text = "Oops"
    }

    return cell!
}

1 个答案:

答案 0 :(得分:0)

我不确定为什么这不适合你。我创建了一个新项目。制作了一个带有tableView的ViewController,并复制/粘贴了你的代码。作品。我得到一个带有开关的单元,另一个有披露,一个没有访问器。你的桌子在工作吗?如果您的应用程序崩溃,那么您的reuseIdentifiers可能会拼写错误或其他内容。也许您没有将tableView作为委托/数据源链接到ViewController。祝你好运!

import Foundation
import UIKit
class MyTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    var cell: UITableViewCell?

    switch indexPath.row {
    case 0:
        cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "enabledCell")
        cell!.textLabel?.text = "Alarm enabled"
        var enabledSwitch = UISwitch(frame: CGRectZero)
        cell!.accessoryView = enabledSwitch
    case 1:
        cell = self.tableView.dequeueReusableCellWithIdentifier("calendarCell", forIndexPath: indexPath) as? UITableViewCell
        cell!.textLabel?.text = "Calendar"
        cell!.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
    default:
        cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? UITableViewCell
        cell!.textLabel?.text = "Oops"
    }

    return cell!
}

}