无法更改UITableViewCell属性
我正在尝试更改表视图单元格的附件视图/附件类型,但它不起作用无法更改UITableViewCell属性,uitableview,swift,Uitableview,Swift,我正在尝试更改表视图单元格的附件视图/附件类型,但它不起作用 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell: UITableViewCell? switch indexPath.row { case 0: cell = UITableViewCell(
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!
}
我不知道为什么这对你不起作用。我创建了一个新项目。制作一个包含tableView的ViewController,并复制/粘贴代码。作品我有一个带开关的电池,另一个有泄露,还有一个没有访问器。你的桌子能用吗?如果你的应用程序崩溃了,可能是你的reuseIdentifier拼错了或者其他什么。可能您没有将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!
}
}