[英]UITableView not loading data
我的數據沒有加載到UITableView
,我不確定為什么,據我所知,我已經完成了本書的所有工作。
我在情節TableViewCell
中創建了TableView
和TableViewCell
,添加了圖像和三個標簽。
結構為: ViewController
> TableView
> TableViewCell
> Image
,標簽
TableViewCell
連接到其TableViewCell.class
甚至沒有加載我的NIB
,原因是我在輸出屏幕上沒有得到任何輸出?
import UIKit
class MyCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var cellTitle: UILabel!
@IBOutlet weak var cellDate: UILabel!
@IBOutlet weak var cellDescription: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var cellImage: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
print ("Did Load")
tableView.delegate = self
tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCellID", for: indexPath)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
}
import UIKit
class MyCell: UITableViewCell{ //<-- make sure your cell has this class and connect the outlets
@IBOutlet weak var cellTitle: UILabel!
@IBOutlet weak var cellDate: UILabel!
@IBOutlet weak var cellDescription: UILabel!
@IBOutlet weak var tableView: UITableView! //<-- what is this doing here?? remove this
@IBOutlet weak var cellImage: UIImageView!
}
class myViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
class Cell{
let title: String
//do this for every UIView in your cell
init(title: String //continue...){
self.title = title
//continue
}
}
var cells = [Cell]()
@IBOutlet weak var myTableView //<-- set here the outlet
override func viewDidLoad() {
print ("Did Load")
cells.append(Cell(//initialize))
myTableView.delegate = self
myTableView.dataSource = self
myTableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCellID", for: indexPath) as! MyCell //<-- ADDED
cell.cellTitle = cells[indexPath.row].title
//continue
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
tableview的所有委托和數據源方法** [** func numberOfSections(在tableView中:UITableView)-> Int,
func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int)-> Int,
func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell,
func tableView(_ tableView:UITableView,heightForRowAt indexPath:IndexPath)-> CGFloat **] **
必須在viewcontroller中,而不在tableviewcell中。
設置委托和數據源應該在視圖控制器中。 tableView出口以及所有委托和數據源方法也應位於視圖控制器中。 嘗試在您的單元格類中創建一個配置單元格方法,在其中設置要用於更新單元格內容視圖中的UI對象的數據,並在單元格中的行處創建單元格-類型轉換為自定義類視圖控制器類中的方法。
class is MyCell : UITableViewCell
但您說的結構是:ViewController> TableView> TableViewCell>圖像,標簽。
您應該在ViewController中添加tableview。
您需要向表格視圖注冊表格單元類或筆尖。 驗證是否已執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.