我试图通过点击它来展开和折叠表格视图单元格。我按照教程https://www.youtube.com/watch?v=LqHzHkuXRxQ&t=89s进行了操作。所有的事情都完全像在这个视频中。它成功运行,没有任何错误或警告。但我无法查看表格视图单元格中的内容。它只显示一个空表。我认为这是Xcode版本的问题。在教程中,我猜他使用了Xcode 7。我正在使用Xcode 8.2。我不知道应该做出哪些更改,以查看表格视图单元格中的内容。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var selectedIndex = -1
var dataArray : [[String:String]] = [["FirstName" : "Arun", "LastName" : "Gupta"],["FirstName" : "John", "LastName" : "Cena"],["FirstName" : "James" , "LastName" : "Bond"],["FirstName" : "Iron", "LastName" : "Man"]]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) ->CGFloat
{
if(selectedIndex == indexPath.row) {
return 100;
}
else{
return 40;
}
}
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell{
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! customCell
let obj = dataArray[indexPath.row]
cell.firstViewLabel.text = obj["FirstName"]
cell.secondViewLabel.text = obj["LastName"]
return cell;
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(selectedIndex == indexPath.row) {
selectedIndex = -1
} else {
selectedIndex = indexPath.row
}
self.tableView.beginUpdates()
self.tableView.reloadRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.automatic)
self.tableView.endUpdates()
}
}
import UIKit
class customCell: UITableViewCell {
@IBOutlet weak var firstView: UIView!
@IBOutlet weak var firstViewLabel: UILabel!
@IBOutlet weak var secondView: UIView!
@IBOutlet weak var secondViewLabel: UILabel!
@IBOutlet weak var secondHeightConstraint: NSLayoutConstraint!
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
}
var showsDetails = false {
didSet {
secondHeightConstraint.priority = showsDetails ? 250 : 999
}
}
}
答案 0 :(得分:2)
您尚未提供UITableViewDelegate
和UITableViewDataSource
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
在viewDidLoad()
:
tableView.dataSource = self
tableView.delegate = self
答案 1 :(得分:0)
如果我看错了,你就错过了numberOfSections(in tableView: UITableView) -> Int
方法。所以你的TableView有0个部分。