我有Xcode 8和swift 3,我的表格视图出现错误。所以一切都按照我喜欢的方式运行我的表视图除了每次运行程序时,顶部都有一个额外的部分或部分标题。在调试器中它显示为两个单独的表视图,我很困惑为什么。下面是我的代码,附上我的程序的截图。在程序中,您可以看到表格视图单元格的顶部是重复的。
-Table View Controller code-
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
var items = ["post1", "post2", "post3"]
var hidden: [Bool] = [true, true]
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var newLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//hide the navigation bar
self.navigationController?.isNavigationBarHidden = true
//register nib
self.tableView.register(UINib(nibName: "TextViewCell", bundle: nil), forCellReuseIdentifier: "TxtViewCel")
//set delegates
self.tableView.delegate = self
self.tableView.dataSource = self
//configure row height
tableView?.estimatedRowHeight = 300
self.tableView.rowHeight = 384
//add tap gesture to dismiss keyboard on click outside
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.hideKeyboard))
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard(){
tableView.endEditing(true)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 384
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
//configure rows in section. Rows are invisible when section is not clicked
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if hidden[section] {
return 0
} else {
return items.count
}
}
//cell for row at index path method
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TxtViewCel", for: indexPath) as! TextViewCell
cell.tView?.text = items[indexPath.row]
return cell
}
//height for header in section
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}
//view for header in section along with tap gesture to hide rows when section is not clicked
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.textAlignment = .left
label.text = "Click to See the News"
label.textColor = UIColor.blue
label.tag = section
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
return label
}
//function for tap gesture
func tapFunction(sender:UITapGestureRecognizer) {
let section = sender.view!.tag
let indexPaths = (0..<3).map { i in return IndexPath(item: i, section: section) }
hidden[section] = !hidden[section]
tableView?.beginUpdates()
if hidden[section] {
tableView?.deleteRows(at: indexPaths, with: .fade)
} else {
tableView?.insertRows(at: indexPaths, with: .fade)
}
tableView?.endUpdates()
}
//did select Row at method (not functioning for some reason)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("hello")
let vc = EditTableViewController()
//let vc = self.storyboard?.instantiateViewController(withIdentifier: "id") as! EditTableViewController
self.navigationController?.pushViewController(vc, animated: true)
}
}
extension UIViewController {
func showAlert(message: String, title: String = "") {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
}
-Table View Cell code-
import UIKit
class TextViewCell: UITableViewCell, UITextViewDelegate
{
//MARK: Properties
@IBOutlet weak var tView: UITextView!
@IBOutlet weak var textViewCell: UIView!
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
}
*/
func configure(texts: String?){
tView.text = texts
tView.textColor = UIColor.green
tView.accessibilityValue = texts
}