【发布时间】:2016-09-13 12:27:21
【问题描述】:
我将 Xcode 7.3 与 iOS 9 和 Swift 一起使用。我希望用户能够在文本字段中输入一个整数,点击一个按钮,然后在同一屏幕上的 tableview 中显示整数。我希望用户能够创建一个整数列表来执行此操作。
我创建了文本字段、按钮和表格视图。编写此函数的最简单方法是什么?
【问题讨论】:
标签: swift list uitableview append
我将 Xcode 7.3 与 iOS 9 和 Swift 一起使用。我希望用户能够在文本字段中输入一个整数,点击一个按钮,然后在同一屏幕上的 tableview 中显示整数。我希望用户能够创建一个整数列表来执行此操作。
我创建了文本字段、按钮和表格视图。编写此函数的最简单方法是什么?
【问题讨论】:
标签: swift list uitableview append
我猜你知道如何使用标准表格视图,这样我就不必发布不相关的代码了?
试试这个.....
单击按钮时使用 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
【讨论】: