Swift TableView
1.拉一個Table View 
2.Table View右方的 Prototype Cells 從0改成1 
3.點選cell 將identifier處輸入Cell  
宣告類別 
UITableViewDelegate 
UITableViewDataSource 
是protocol 
protocol算是一種需求 
需要UITableView顯示出來需要開出需求清單 
例如: 
表格裡面你要繪製出多少列,裡面要提供多少資料? 
而可以藉由代理物件delegate object 
delegation pattern 
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 補上這段程式碼
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //回傳總列數
        //總共看陣列有幾個物件就回傳幾列
        return studentNames.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        //重複利用表格內用有的cell但顯示不同的物件
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
        //設定Cell
        cell.textLabel?.text = studentNames[indexPath.row]
        return cell
    }
}
var studentNames = ["Daris", "Min-Yeh", "Lazy", "Lin-yu", "Monica", "Jason", "Joe"] 
Comments
Post a Comment