【问题标题】:How to append a cell to a tableview using iOS 9?如何使用 iOS 9 将单元格附加到表格视图?
【发布时间】:2016-09-13 12:27:21
【问题描述】:

我将 Xcode 7.3 与 iOS 9 和 Swift 一起使用。我希望用户能够在文本字段中输入一个整数,点击一个按钮,然后在同一屏幕上的 tableview 中显示整数。我希望用户能够创建一个整数列表来执行此操作。

我创建了文本字段、按钮和表格视图。编写此函数的最简单方法是什么?

【问题讨论】:

    标签: swift list uitableview append


    【解决方案1】:

    我猜你知道如何使用标准表格视图,这样我就不必发布不相关的代码了?

    试试这个.....

    单击按钮时使用 beginUpdates 和 endUpdates 插入新单元格..

    首先在表格视图数组中追加数据,假设包含表格视图内容的数组名为Yourarray

    Yourarray.push(0815)  // some integer, which you will grap from the input
    

    然后更新表格并插入新行

    // Update Table Data
    tblname.beginUpdates()
    tblname.insertRowsAtIndexPaths([
        NSIndexPath(forRow: Yourarray.count-1, inSection: 0)
        ], withRowAnimation: .Automatic)
    tblname.endUpdates()
    

    您可以看到带有Yourarray.count-1 的代码行。在那里,您可以在要附加新单元格的行中写入任何整数。 如果你使用的是 tableview,你应该使用一个数组来填充 tableview,我希望你这样做。

    编辑

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        ...
    
    }
    

    首先你从UITableViewDelegate 和UITableViewDataSource 中采用你的课程。

    然后你在你的类中添加这些函数:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0 // how many tableview cells you want to display
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) {
    
            var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell // "cell" is the identifier in this case, change that in your storyboard
    
            cell.textLabel?.text = "Your value for the tableview"
    
            return cell
    }
    

    在 viewDidLoad 中,您必须添加它才能连接它:

    self.zipTable.delegate = self
    self.zipTable.dataSource = self
    

    【讨论】:

    • 感谢您的回复,我会试试这个。现在我唯一编写的代码是创建从 View Controller 到 ViewController.swift 文件的连接。我不知道从哪里开始。
    • 你知道如何制作tableView吗?如果您需要帮助,我会在任何地方为您提供帮助
    • 是的,你有tableViewController还是里面有tableView的ViewController?
    • 我有一个 ViewController,里面有一个 tableView。这是我的 tableView:@IBOutlet weak var zipTable: UITableView!
    • 明白了吗?遵循一些教程,但在一个新问题中提出。您想知道如何附加单元格吗?还是您的问题是如何制作 tableView?
    猜你喜欢
    • 2018-07-02
    • 2015-11-07
    • 2021-06-04
    • 2011-07-09
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    相关资源
    最近更新 更多