2016-05-17 60 views
0

我使用iOS 9和Swift的Xcode 7.3。我希望用户能够在文本字段中输入一个整数,点击一个按钮,然后在同一屏幕上的tableview中显示整数。我希望用户能够创建一个完整的列表。如何使用iOS 9将单元格追加到tableview?

我创建了我的文本框,按钮和tableview。什么是编码这个功能最简单的方法?

回答

0

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

试试这个.....

使用beginUpdates以及当按钮点击插入新电池..

首先附加在您的tableview阵列数据endUpdates,假设包含内容的阵列你的表视图名为Yourarray

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

然后upate你的表,并插入新行

// 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 { 

    ... 

} 

首先你UITableViewDelegateUITableViewDataSource采用类。

然后添加这些功能的类中:

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 
} 

enter image description here

的viewDidLoad中里面,你必须为了添加此把它挂:

self.zipTable.delegate = self 
self.zipTable.dataSource = self 
+0

感谢您的回复,我会试试这个。现在我唯一编码的是创建从View Controller到ViewController.swift文件的连接。我不知道从哪里开始。 – Impulso

+0

你知道如何制作tableView吗?如果你需要一些帮助,我会在任何地方帮助你 – Anokrize

+0

你能展示如何添加tableView吗? – Impulso

相关问题