2014-09-17 4 views
0

Я пытаюсь изменить вид аксессуаров/аксессуаров для своих ячеек таблицы, но он не работает.Невозможно изменить свойства 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! 
} 

ответ

0

Я не уверен, почему это не работает для вас. Я создал новый проект. Создал ViewController с помощью tableView в нем и скопировал/вставлял ваш код. Работает. Я получаю 1 ячейку с коммутатором, другую с раскрытием и одну без доступа. Работает ли ваш стол вообще? Если ваше приложение просто сработает, возможно, ваши повторное использованиеИдентификаторы ошибочно написаны или что-то в этом роде. Возможно, вы не связали свой 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! 
} 

}

+0

это то, что я вижу. http://i.imgur.com/eUTncRr.png – user3347446

Смежные вопросы