如何在UIViewController中填充UITableView? UITableView是動態的並使用原型單元格。將數據/行添加到ViewController中的UITableView。 Swift
(使用SWIFT)
如何在UIViewController中填充UITableView? UITableView是動態的並使用原型單元格。將數據/行添加到ViewController中的UITableView。 Swift
(使用SWIFT)
請執行以下操作:
import UIKit
class tableViewOne: UIViewController{
@IBOutlet var tableView: UITableView!
// your data
var dummyData = ["data 0","data 1","data 2"]
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension tableViewOne: UITableViewDataSource, UITableViewDelegate {
// Define no of rows in your tableView
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dummyData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Default cell
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell
cell.textLabel!.text = dummyData[indexPath.row]
return cell;
}
}
注:不要忘記設置DataSource
和UITableView
Delegate
在storyboard
。如果已經在storyboard
中定義,則無需以編程方式定義。
您需要按Ctrl +拖動您的table view
到您的視圖控制器。
在您的視圖控制器內viewDidLoad()
,您將需要設置委託和數據源:
tableView.dataSource = self
tableView.delegate = self
您的視圖控制器需要實現:
class YourViewController : UITableViewDelegate, UITableViewDataSource
現在,您的View Controller類將需要覆蓋返回單元格/行數等的相關方法。
祝你好運。
添加更多信息。你需要2個原型單元來做什麼? – MShah