swift - 快速在 UITableView 中创建单元格

标签 swift uitableview

我的问题是,如何在 UITableView 中创建动态原型(prototype)单元格。我在互联网上浏览了一下,只找到了 UITableViewControllers 的教程和代码,但没有找到我想在我的应用程序中使用的 UITableViews。因此,如果有人可以帮忙编写一些代码,那就太好了。

谢谢。

最佳答案

这是创建 UITableView 的代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


    @IBOutlet weak var tableView: UITableView!

    let tableData = ["1","2","3","4"]
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self

        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

        return tableData.count
    }



    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

        var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

        cell.textLabel?.text = self.tableData[indexPath.row]

        return cell
    }
}

关于swift - 快速在 UITableView 中创建单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31864115/

相关文章:

ios - UITableView 内的 UISearchbar 作为隐藏键盘的最后一个单元格

ios - UITableView 单元格标识符字段被禁用

ios - 将 UITextView 滚动到特定文本

swift - 更改 SKShapeNode 的 lineJoin

swift - 矩阵中不同列中的快速更改值更改了另一个

ios - 在 TableView 之外配置自定义 TableViewCell?

ios - Swift:如何在使用多个 TableViewCell 类时偏移 tableView 上的索引?

swift - Swift 中的条件 if 语句

iOS 如何检测后台通话状态?

ios - 使用storyboard时加载不同的UITableViewCell