【问题标题】:Swift Xcode6 UITableView Prototype CellSwift Xcode6 UITableView 原型单元
【发布时间】:2015-11-21 02:34:46
【问题描述】:

我正在使用 UITableView 做一个项目,但我不知道如何向其中添加数据。

现在我正在构建我的第一个视图,但出现了问题。有人可以帮助我吗?

代码如下:

import UIKit  

class WorkersListTableViewController: UITableViewController {

    var numOfWorkersNames = []

    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 Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return numOfWorkersNames.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("WorkersCell", forIndexPath: indexPath) as! UITableViewCell  
        // Configure the cell...
        cell. NameOfTheWorkerLabel.text = numOfWorkersNames[indexPath.row] as String
        return cell
    }


    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO 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 NO 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.
    }
    */

}

现在是工人小组:

class WorkersCellTableViewCell: UITableViewCell {

    @IBOutlet weak var NameOfTheWorkerLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

}

【问题讨论】:

  • 您需要在numOfWorkersNames 中输入一些名称。单元格的数量取决于您拥有的条目数以及您的代码为 0。
  • 去UITableView教程吧

标签: ios uitableview prototype cell


【解决方案1】:

请说明什么问题和添加什么数据到tableView

如果问题是应用启动时只有一个空白视图,您应该在 numOfWorkersNames

中添加一些字符串

例如:var numOfWorkersNames = ["worker1","worker2","worker3"]

那么numberOfRowsInSection会得到数组的个数为3,cellForRowAtIndexPath可以从numOfWorkdersNames数组中得到它的内容

【讨论】:

  • 我不想创建一个设置一些预定义项目的数组。工人姓名将添加到另一个视图中。使用 UITextField。
  • 所以只需向该数组添加一些数据。例如:您在其他地方有一个文本字段和一个按钮。当您单击按钮时,它会将文本字段中的内容添加到您的工作数组中,因此在您的按钮单击功能中,您可以编写 numOfWorkersNames.append(textfield.text) tableView.reloadData()
猜你喜欢
  • 2014-12-02
  • 2020-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2016-06-10
  • 2014-05-18
相关资源
最近更新 更多