Swift UIView 中的 TableView 不显示数据
Swift TableView in a UIView not displaying data
我想创建一个页面,该页面应该在顶部有一个地图,在底部有 5 行表格视图。所以我创建了 UIViewController 放置我的地图视图然后放置 TableView。我创建了 myViewController.swift 和 myTableViewCell.swift
但是当我在模拟器上尝试时,tableview 上没有显示任何数据。只有空 cells.I 没有收到错误
这是我的代码。谢谢
import UIKit
import MapKit
class myViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var mapView: MKMapView!
var labels = ["some arrays"]
var images = ["some image names in an array"]
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return labels.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("mySegue", forIndexPath: indexPath) as! myTableViewCell
cell.myCellLabel.text = labels[indexPath.row]
cell.myCellImage.image = UIImage(named: images[indexPath.row])
return cell
}
}
试试这个:
override func viewDidLoad() {
super.viewDidLoad()
// Add this
tableView.delegate = self
tableView.dataSource = self
}
你应该做两种方式中的一种:
tableView.delegate = self
tableView.dataSource = self
在您 viewController 的 viewDidLoad() 中
要么
- select tableView 并通过按住控件拖动到 viewController,首先 select 数据源,然后再次 select 委托。
设置tableview的dataSource。
tableview.dataSource = 自己
tableview.delegate = 自己
通过storyboard分配UITableView/UICollectionView的delegate和datasource,减少代码如下:
右键单击您的 UITableView,然后单击代表/数据源的圆圈,然后转到 viewcontroller。为清楚起见,请参见下图。
我想创建一个页面,该页面应该在顶部有一个地图,在底部有 5 行表格视图。所以我创建了 UIViewController 放置我的地图视图然后放置 TableView。我创建了 myViewController.swift 和 myTableViewCell.swift
但是当我在模拟器上尝试时,tableview 上没有显示任何数据。只有空 cells.I 没有收到错误
这是我的代码。谢谢
import UIKit
import MapKit
class myViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var mapView: MKMapView!
var labels = ["some arrays"]
var images = ["some image names in an array"]
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return labels.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("mySegue", forIndexPath: indexPath) as! myTableViewCell
cell.myCellLabel.text = labels[indexPath.row]
cell.myCellImage.image = UIImage(named: images[indexPath.row])
return cell
}
}
试试这个:
override func viewDidLoad() {
super.viewDidLoad()
// Add this
tableView.delegate = self
tableView.dataSource = self
}
你应该做两种方式中的一种:
tableView.delegate = self
tableView.dataSource = self
在您 viewController 的 viewDidLoad() 中 要么 - select tableView 并通过按住控件拖动到 viewController,首先 select 数据源,然后再次 select 委托。
设置tableview的dataSource。
tableview.dataSource = 自己 tableview.delegate = 自己
通过storyboard分配UITableView/UICollectionView的delegate和datasource,减少代码如下:
右键单击您的 UITableView,然后单击代表/数据源的圆圈,然后转到 viewcontroller。为清楚起见,请参见下图。