簡體   English   中英

swift3中的tableview單元格

[英]tableview cell in swift3

因為我是iOS swift3.0的初學者

我正在嘗試使用swift3中的xib單元創建簡單的表格視圖。我已經看過一些教程,但是我想找到正確的例子。 所以有人可以幫助我。

請找到我下面的代碼

class Myclass: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tabelviewoutlet: UITableView!
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
let cellIdentifier = "Cell"


override func viewDidLoad() {
    super.viewDidLoad()  
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.items.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "td")! as UITableViewCel
}

請找到代碼,並讓我知道。

import UIKit
class ViewController: UIViewController,UITableViewDataSource{


    @IBOutlet weak var tabelviewoutlet: UITableView!
    let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
    let cellIdentifier = "Cell"


    override func viewDidLoad() {
        super.viewDidLoad()

          tabelviewoutlet.dataSource = self

        tabelviewoutlet.register(UINib(nibName: "aTableViewCell", bundle: nil), forCellReuseIdentifier: "Cell")


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return animals.count
    }

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! aTableViewCell
        //cell.albl?.text = self.animals[indexPath.row]
       cell.albl.text=self.animals[indexPath.row]

        return cell
    }
}

使用這樣的代碼,

class Myclass: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tabelviewoutlet: UITableView!

    let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // register your xib
        self.tblMyCustom.register(UINib(nibName: "CustomTableCell", bundle: nil), forCellReuseIdentifier: "customCell")
        self.tblMyCustom.delegate = self
        self.tblMyCustom.datasource = self
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.animals.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let customCell: CustomTableCell! = tableView.dequeueReusableCell(withIdentifier: "customCell") as? CustomTableCell
        customCell.myLabel.text = self.animals[indexPath.row]
        return customCell
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM