我是Swift的新手。 我尝试制作一个自定义的TableView并在StoryBoard中添加了一个原型单元
这是它的代码:
class SearchTableViewCell: UITableViewCell {
@IBOutlet var routeDescriptionLbl: UILabel!
@IBOutlet var ivRoute: UIImageView!
@IBOutlet var distanceLbl: UILabel!
@IBOutlet var routeNumberLbl: UILabel!
override func awakeFromNib() {
//still empty yet
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
这里是整个视图的视图控制器(部分):
class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
var transportData : [RouteModel] = []
override func viewDidLoad() {
super.viewDidLoad()
fillMockup()
self.lvMain.reloadData() //in case it's needed after mockup is filled
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
NSLog("viewDidAppear")
}
func fillMockup(){
//just some mockup data to test if it's working
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "44", description: "Ярославль - Сан Тропе",dist: "200 m"))
transportData.append(RouteModel(type: RouteModel.TYPE_TRAM,routeNumber: "11", description: "Ярославль - Сан Тропе",dist: "400 m"))
transportData.append(RouteModel(type: RouteModel.TYPE_R_BUS,routeNumber: "1", description: "Ярославль Главный - Жопа Мира",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_TROLLEY,routeNumber: "24", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43а", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "85б", description: "Ярославль - Брагино",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "38", description: "Ярославль - Брагино",dist: "2.4 км"))
}
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
NSLog("transportData.count = \(transportData.count)")
return transportData.count
}
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as? SearchTableViewCell
if let cell = cell {
cell.routeNumberLbl.text = transportData[indexPath.row].routeNumber
cell.routeDescriptionLbl.text = transportData[indexPath.row].description
cell.distanceLbl.text = transportData[indexPath.row].dist
return cell
}
return UITableViewCell()
}
func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath){
NSLog("You selected cell #\(indexPath.row)!")
let position = indexPath.row;
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
}
现在,没有调用任何来自表视图的Log。当然,TableView没有显示
问题是什么?
答案 0 :(得分:1)
这种情况正在发生,因为您的单元格总是创建一个新实例,因为它不会进入部分。因此,检查您的单元格是否为零,然后像这样创建一个单元格实例..
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell: MyCell! = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")
if cell == nil {
tableView.registerNib(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")
cell =tableView.dequeueReusableCellWithIdentifier(identifier) as? MyCell
}
return cell
}
}
此处 MyCell 是自定义单元格。
答案 1 :(得分:1)
在viewDidLoad()
函数
self.yourtableviewname.delegate = self
self.yourtableviewname.dataSource = self
答案 2 :(得分:0)
喜欢
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: SearchTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as! SearchTableViewCell
cell.routeNumberLbl.text = transportData[indexPath.row].routeNumber
cell.routeDescriptionLbl.text = transportData[indexPath.row].description
cell.distanceLbl.text = transportData[indexPath.row].dist
return cell
}
更新回答
override func viewDidLoad() {
super.viewDidLoad()
self.lvMain.delegate = self // invoke data source and delegate on current class
self.lvMain.dataSource = self
fillMockup()
}
func fillMockup(){
//just some mockup data to test if it's working
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "44", description: "Ярославль - Сан Тропе",dist: "200 m"))
transportData.append(RouteModel(type: RouteModel.TYPE_TRAM,routeNumber: "11", description: "Ярославль - Сан Тропе",dist: "400 m"))
transportData.append(RouteModel(type: RouteModel.TYPE_R_BUS,routeNumber: "1", description: "Ярославль Главный - Жопа Мира",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_TROLLEY,routeNumber: "24", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43а", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "85б", description: "Ярославль - Брагино",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "38", description: "Ярославль - Брагино",dist: "2.4 км"))
self.lvMain.reloadData() //reload the table when does loaded perfectly
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: SearchTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as! SearchTableViewCell
cell.routeNumberLbl.text = transportData[indexPath.row].routeNumber
cell.routeDescriptionLbl.text = transportData[indexPath.row].description
cell.distanceLbl.text = transportData[indexPath.row].dist
return cell
}
有关其他示例,请参阅this
答案 3 :(得分:0)
您可以通过以下两个步骤检查是否忘记执行任何操作
1)交叉检查您是否已连接Datasource,Delegate和IBoutlet for tableview?
2)检查Storyboard中的设置标识符是否与reuseIdentifier相同?
答案 4 :(得分:0)
您需要在viewDidLoad中或从Stroyboard中自定义tableView委托和数据源。
override func viewDidLoad() {
super.viewDidLoad()
tableViewName.delegate = self
tableViewName.dataSource = self
}