使用Swift在Xcode中填充TableView

时间:2016-05-18 09:32:36

标签: swift tableview

我在Xcode中有一个ViewController,其中我添加了两个按钮,一个视图和一个TableView (不是表视图控制器)。

我希望在表格中有更多行,我可以在其中添加一些日期,这些日期有时会发生变化。

现在我有一个问题: 我有一节课,我将控制那些东西:

import UIKit

class DeviceTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 4
    }

    /*
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

现在我想将它与视图连接起来。这就是为什么我想在属性检查器中将tableView的类更改为DeviceTableViewController(我的类的名称)。但是Xcode不会这样做。

我怎么能改变它?

1 个答案:

答案 0 :(得分:2)

UITableViewController只能处理Apple提供的标准表格控制器。为了创建一个具有您想要的混合视图的控制器,您需要继承标准UIViewController而不是UITableViewController(您目前已经完成)。

我尝试重新创建与您类似的场景:具有UIView,两个UIButtonsUITableView的控制器。请注意,表格视图的datasourcedelegate已在故事板中设置,但您可以在代码中执行相同操作。

//
//  ViewController.swift
//  TableWithStuff
//
//  Created by Stefan Veis Pennerup on 18/05/16.
//  Copyright © 2016 Kumuluzz. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    // MARK: - Models

    let model = ["Coffee", "With", "Milk", "Please"]

    // MARK: - Storyboard outlets

    @IBOutlet weak var firstButton: UIButton!
    @IBOutlet weak var secondButton: UIButton!
    @IBOutlet weak var myView: UIView!
    @IBOutlet weak var myTableView: UITableView!

    // MARK: - Storyboard actions

    @IBAction func firstButtonTapped(sender: UIButton) {
        myView.backgroundColor = UIColor.redColor()
    }

    @IBAction func secondButtonTapped(sender: AnyObject) {
        myView.backgroundColor = UIColor.greenColor()
    }

    // MARK: - UITableViewDataSource

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return model.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myAwesomeCell")!
        cell.textLabel!.text = model[indexPath.row]
        return cell
    }

}

Storyboard of the controller

Datasource and delegate set in the storyboard

The final result