Ios 表视图中重复的节或节标题
我有Xcode 8和swift 3,我的表视图出现错误。因此,在我的表视图中,除了每次运行我的程序时,顶部都有一个额外的节或节标题外,其他一切都正常工作。在调试器中,它显示为两个独立的表视图,我不知道为什么。下面是我的代码,附件是我的程序截图。在程序中,您可以看到“表格视图”单元格的顶部是重复的Ios 表视图中重复的节或节标题,ios,swift,xcode,uitableview,tableview,Ios,Swift,Xcode,Uitableview,Tableview,我有Xcode 8和swift 3,我的表视图出现错误。因此,在我的表视图中,除了每次运行我的程序时,顶部都有一个额外的节或节标题外,其他一切都正常工作。在调试器中,它显示为两个独立的表视图,我不知道为什么。下面是我的代码,附件是我的程序截图。在程序中,您可以看到“表格视图”单元格的顶部是重复的 -Table View Controller code- import UIKit class ViewController: UIViewController, UITableViewDataSou
-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
}
-表视图控制器代码-
导入UIKit
类ViewController:UIViewController、UITableViewDataSource、UITableViewDelegate{
变量项=[“post1”、“post2”、“post3”]
变量隐藏:[Bool]=[true,true]
@IBVAR表格视图:UITableView!
@IBVAR新标签:UILabel!
重写func viewDidLoad(){
super.viewDidLoad()
//隐藏导航栏
self.navigationController?.isNavigationBarHidden=true
//寄存器笔尖
self.tableView.register(UINib(nibName:“TextViewCell”,bundle:nil),强制重用标识符:“TxtViewCel”)
//集合代表
self.tableView.delegate=self
self.tableView.dataSource=self
//配置行高
tableView?.estimatedRowHeight=300
self.tableView.rowHeight=384
//添加点击手势,在单击外部时关闭键盘
让Tap手势=UITapGestureRecognitor(目标:自我,动作:#选择器(ViewController.hideKeyboard))
tappostate.cancelsTouchesInView=true
tableView.AddGestureRecognitor(点击手势)
}
func hideKeyboard(){
tableView.endEditing(真)
}
func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
返回384
}
覆盖函数视图将出现(uo动画:Bool)
{
超级。视图将显示(动画)
}
func numberOfSections(在tableView:UITableView中)->Int{
返回1
}
//配置节中的行。未单击节时,行不可见
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
如果隐藏[部分]{
返回0
}否则{
返回项目。计数
}
}
//索引路径方法处的行的单元格
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“TxtViewCel”,用于:indexPath)作为!TextViewCell
cell.tView?.text=项[indexPath.row]
返回单元
}
//节段中收割台的高度
func tableView(u tableView:UITableView,heightForHeaderInSection:Int)->CGFloat{
返回30
}
//查看节中的标题以及未单击节时隐藏行的点击手势
func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView{
let label=UILabel()
label.textAlignment=.left
label.text=“单击以查看新闻”
label.textColor=UIColor.blue
label.tag=节
让点击=UITapGestureRecognitor(目标:自我,操作:#选择器(ViewController.tapFunction))
label.isUserInteractionEnabled=true
label.AddGestureRecognitor(点击)
退货标签
}
//轻触手势功能
func tapFunction(发送方:UITapgestureRecognitizer){
let section=sender.view!.tag
让indexPaths=(0..您能显示“文档大纲”吗您的界面生成器中的节?您可以检查此项。此问题是因为点击手势。它使您的标签每次都保持活动状态。尝试使用按需设置样式的按钮,而不是使用点击手势标记。使用按钮而不是标签并不能纠正我的问题,尽管这是一个很好的建议。上面的链接也不能显示您的界面生成器中的“文档大纲”部分?您可以检查此部分。此问题是由于点击手势造成的。它使您的标签始终处于活动状态。尝试使用按需样式的按钮,而不是使用点击手势进行标签。使用按钮而不是标签并不能纠正我的问题,尽管这是一个很好的建议。上面的链接也没有纠正我的问题