2016-05-17 32 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

相關問題