【发布时间】:2019-10-07 18:43:42
【问题描述】:
我在那个单元格中有表格视图我在那个标题中创建了标题视图我有标签和按钮,我从数组中获取标签值,并且该数组计算了可以正常工作的部分的数量。但是当我点击按钮时,我需要展开单元格,并且在那个展开的单元格中我需要添加标签,但是当我点击按钮时,它会说
错误:索引超出范围
在numberOfRowsInSection 中并没有展开。
这是我的代码:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, DataSendingDelegateProtocol, FieldsDelegateProtocol {
var shouldCellBeExpanded:Bool = false
var indexOfExpendedCell:NSInteger = -1
// let kHeaderSectionTag: Int = 6900;
@IBOutlet weak var tableView: UITableView!
var sectionArray = [String]()
var sectionItemsArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
//self.tableView!.tableFooterView = UIView()
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func sendDataToTableRow(myData: String) {
self.sectionItemsArray.append(myData)
tableView.reloadData()
// print(iteamsArray)
}
func sendDataToSectionLabel(myData: String) {
self.sectionArray.append(myData)
tableView.reloadData()
print(sectionArray)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "segue") {
let vc = segue.destination as! PopViewController
vc.delegate = self
}
else if (segue.identifier == "fieldsSegue") {
let vc = segue.destination as! FieldsViewController
vc.delegate = self
}
}
@objc func expandButnClicked(sender:UIButton){
print("expand button tapped")
shouldCellBeExpanded = !shouldCellBeExpanded
indexOfExpendedCell = sender.tag
if shouldCellBeExpanded {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
else {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
}
@IBAction func addBtn(_ sender: Any) {
}
// MARK: - Tableview Methods
func numberOfSections(in tableView: UITableView) -> Int {
return sectionArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.indexOfExpendedCell == section) {
let arrayOfItems = self.sectionItemsArray[section]
return arrayOfItems.count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if (self.sectionArray.count != 0) {
return self.sectionArray[section] as? String
}
return ""
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44.0;
}
/*func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat{
return 0;
}*/
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor.colorWithHexString(hexStr: "#0075d4")
header.textLabel?.textColor = UIColor.white
let button = UIButton(frame: CGRect(x: 380, y: 10, width: 15, height: 15))
button.backgroundColor = UIColor.green
button.tag = section
button.addTarget(self, action: #selector(expandButnClicked), for: .touchUpInside)
header.addSubview(button)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if shouldCellBeExpanded && indexPath.row == indexOfExpendedCell {
return 200
}
else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! TableViewCell
cell.textLab?.textColor = UIColor.black
cell.textLab.text = sectionItemsArray[indexPath.row]
cell.backgroundColor = UIColor.black
return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
请帮我写代码。
【问题讨论】:
-
在 sectionItemsArray 中添加新索引,点击按钮。
-
@RajeshKumarR 对于不同的单元格没有不同的 sectionItemsArray
-
@RajeshKumarR sectionItemsArray 数据将来自 sendDataToTableRow 委托方法
-
sectionItemsArray 类型应为
[[String]]。例如:var sectionArray: [String] = ["1","2"] var sectionItemsArray:[[String]] = [["a","b"],["c","d"]] -
@RajeshKumarR ["a", "b", "c"] var sectionArray: [String] = ["1","2"]
标签: ios swift uitableview uibutton