因为我是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
}
答案 0 :(得分:3)
请找到代码并告诉我。
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
}
}
答案 1 :(得分:0)
使用这样的代码,
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
}
}