自定义 Table 单元格不显示任何内容- Swift 2
Custom Table Cell doesn't show any content- Swift 2
我正在尝试制作自定义 table 视图单元格,以便在我按下应用程序注释上的按钮时显示。当我按下按钮时,出现一个空单元格,我无法找出它有什么问题。你有什么建议?
提前致谢
class ViewController: UIViewController,
MKMapViewDelegate、CLLocationManagerDelegate、UIPopoverPresentationControllerDelegate、UITableViewDataSource、UITableViewDelegate {
struct MyData {
var imagy:UIImage
var title:String
var details:String
}
var tableData: [MyData] = []
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var mapItemData:MKMapItem!
let textCellIdentifier = "TextCell"
//TableViewDataSource functions
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{ return 1 }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Create a new cell with the reuse identifier of our prototype cell
// as our custom table cell class
let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as! TableViewController
// Set the first row text label to the firstRowLabel data in our current array item
cell.imagy.image = tableData[indexPath.row].imagy
// Set the second row text label to the secondRowLabel data in our current array item
cell.title.text = tableData[indexPath.row].title
// Set the second row text label to the secondRowLabel data in our current array item
cell.details.text = tableData[indexPath.row].details
// Return our new cell for display
tableView.delegate = self
tableView.dataSource = self
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?){
// Set any data to be shown on the table view here
}
var picture:[UIImage] = [
UIImage(named: "pic1.jpg")!,
//UIImage(named: "pic2.jpg")!,
//UIImage(named: "pic3.jpg")!,
//UIImage(named: "pic4.jpg")!,
// UIImage(named: "pic5.jpg")!,
// UIImage(named: "pic6.jpg")!,
//UIImage(named: "pic7.jpg")!,
UIImage(named: "pic8.jpg")!, ]
override func viewDidLoad() {
super.viewDidLoad()
这是 TableView Class:
class 表ViewController: UITableViewCell {
@IBOutlet weak var imagy: UIImageView!
@IBOutlet weak var title: UILabel!
@IBOutlet weak var details: UILabel!
@IBOutlet weak var exit: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
cellForRowAtIndexPath
仅在您的 tableView.dataSource
已设置时调用。你把这个:
tableView.delegate = self
tableView.dataSource = self
在 cellForRowAtIndexPath
中,所以您的 table 中当然没有任何内容。这就像你把钥匙放在房子里然后关上门。将它们放在 viewDidLoad
中。
我正在尝试制作自定义 table 视图单元格,以便在我按下应用程序注释上的按钮时显示。当我按下按钮时,出现一个空单元格,我无法找出它有什么问题。你有什么建议? 提前致谢
class ViewController: UIViewController, MKMapViewDelegate、CLLocationManagerDelegate、UIPopoverPresentationControllerDelegate、UITableViewDataSource、UITableViewDelegate {
struct MyData {
var imagy:UIImage
var title:String
var details:String
}
var tableData: [MyData] = []
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var mapItemData:MKMapItem!
let textCellIdentifier = "TextCell"
//TableViewDataSource functions
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{ return 1 }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Create a new cell with the reuse identifier of our prototype cell
// as our custom table cell class
let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as! TableViewController
// Set the first row text label to the firstRowLabel data in our current array item
cell.imagy.image = tableData[indexPath.row].imagy
// Set the second row text label to the secondRowLabel data in our current array item
cell.title.text = tableData[indexPath.row].title
// Set the second row text label to the secondRowLabel data in our current array item
cell.details.text = tableData[indexPath.row].details
// Return our new cell for display
tableView.delegate = self
tableView.dataSource = self
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?){
// Set any data to be shown on the table view here
}
var picture:[UIImage] = [
UIImage(named: "pic1.jpg")!,
//UIImage(named: "pic2.jpg")!,
//UIImage(named: "pic3.jpg")!,
//UIImage(named: "pic4.jpg")!,
// UIImage(named: "pic5.jpg")!,
// UIImage(named: "pic6.jpg")!,
//UIImage(named: "pic7.jpg")!,
UIImage(named: "pic8.jpg")!, ]
override func viewDidLoad() {
super.viewDidLoad()
这是 TableView Class:
class 表ViewController: UITableViewCell {
@IBOutlet weak var imagy: UIImageView!
@IBOutlet weak var title: UILabel!
@IBOutlet weak var details: UILabel!
@IBOutlet weak var exit: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
cellForRowAtIndexPath
仅在您的 tableView.dataSource
已设置时调用。你把这个:
tableView.delegate = self
tableView.dataSource = self
在 cellForRowAtIndexPath
中,所以您的 table 中当然没有任何内容。这就像你把钥匙放在房子里然后关上门。将它们放在 viewDidLoad
中。