2015-07-26 4 views
2

я данные сохранены, как это:Загрузка данных в UITableView

var mineSpillere = ["erik", "tom", "phil"] 

Как я могу добавить, что данные в UITableView, нажав UIButton как это ?:

@IBAction func backButtonAction(sender: AnyObject) { 
    // Press this button here to add the "mineSpillere" data to myTableView 
} 
+0

При нажатии на кнопку, добавить вид таблицы и установить делегат. – Amit89

+0

@ Amit89 - Добавить табличный вид? Вид таблицы уже существует, но он пуст. Я хочу добавить данные в действие кнопки. –

ответ

0

Установите источник данных вид таблицы внутри IBAction. так как это:

@IBAction func backButtonAction(sender: AnyObject) { 
    myTableView.dataSource = self 
    // Loading from NSUserDefaults: 
    // Please name it something better than array I couldn't come up with any names. 
    if let array = NSUserDefaults.standardUserDefaults().arrayForKey("key") as? [String] 
    { 
      // The key exists in user defaults 
    } 
    else 
    { 
      // The key doesn't exist in the user defaults, do some error handling here. 
    } 
} 

, а затем реализовать источник данных:

extension MyVC : UITableViewDataSource 
{ 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    { 
     return 1 
    } 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return mineSpillere.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = UITableViewCell(style: .Default, reuseIdentifier: "cell") 
     cell.textLabel!.text = mineSpillere[indexPath.row] 
     return cell 
    } 
} 
+0

Что такое "расширение MyVC: UITableViewDataSource"? –

+0

MyVC - это в основном контроллер вида, с которым вы работаете, где находится этот TableView. Класс, в котором присутствует функция IBAction. –

+0

Как это сделать, если у меня есть это: var mineSpillere = [String](), и данные сохраняются на ключ с помощью NSUserDefault? –

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