【问题标题】:Make first section rows cell already expanded in Expandable table view cells使第一部分行单元格已在可扩展表格视图单元格中展开
【发布时间】:2018-03-09 11:10:14
【问题描述】:

在我的应用程序中,我实现了可扩展的表格视图。它工作得很好,但现在我想更改第一部分已经是可扩展模式,但我无法这样做。

在这里,我实现了自己的本机代码,用于创建可扩展的 tableview,而不使用任何第三方库。

我在这里发布我的可扩展 TableView 的完整代码:

@IBOutlet weak var tableViewSecond: UITableView!
var hidden = [true]

override func viewDidLoad() {
    super.viewDidLoad()
    tableViewSecond.delegate = self
    tableViewSecond.dataSource = self
    InspectionArray = [["inspection_name":"AVM Inspection"], ["inspection_name":"Simple Inspection"], ["inspection_name":"BVM Inspection"]]
    InspectionSectionArray = [["inspection_Session":"Current Inspection"], ["inspection_Session":"Past Inspection"], ["inspection_Session":"Future Inspection"]]
}

func numberOfSections(in tableView: UITableView) -> Int {
    for _ in 0..<InspectionSectionArray.count {
        hidden.append(true)
    }
    return InspectionSectionArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if hidden[section] {
        return 0
    } else {
        return InspectionArray.count
    }
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView()
    headerView.backgroundColor = UIColor.orange
    headerView.tag = section
    
    let label = UILabel()
    label.text = (InspectionSectionArray[section] as AnyObject).value(forKey: "inspection_Session") as? String
    label.frame = CGRect(x: 45, y: 5, width: 150, height: 35)
    label.font = UIFont.boldSystemFont(ofSize: 15)
    headerView.addSubview(label)
    label.tag = section
    
    let tapForheaderView = UITapGestureRecognizer(target: self, action: #selector(SecondViewController.tapFunction))
    headerView.isUserInteractionEnabled = true
    headerView.addGestureRecognizer(tapForheaderView)
    
    return headerView
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 2
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 45
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "SecondTableViewCell", for: indexPath) as? SecondTableViewCell
    if cell == nil {
        cell = UITableViewCell(style: .default, reuseIdentifier: "SecondTableViewCell") as? SecondTableViewCell;
    }
    cell!.dataLbl.text = (InspectionArray[indexPath.row] as AnyObject).value(forKey: "inspection_name") as? String
    return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    print(indexPath.row)
    print("\(indexPath.section)","\(indexPath.row)")
}

func tapFunction(sender:UITapGestureRecognizer) {
    let section = sender.view!.tag
    let indexPaths = (0..<InspectionArray.count).map { i in return IndexPath(item: i, section: section)  }
    hidden[section] = !hidden[section]
    tableViewSecond.beginUpdates()
    if hidden[section] {
        tableViewSecond.deleteRows(at: indexPaths, with: .fade)
    } else {
        tableViewSecond.insertRows(at: indexPaths, with: .fade)
    }
    tableViewSecond.endUpdates()
}

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    我相信你只需要改变这个:

    // declare hidden as this
    var hidden: [Bool] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        InspectionArray = [["inspection_name":"AVM Inspection"], ["inspection_name":"Simple Inspection"], ["inspection_name":"BVM Inspection"]]
        InspectionSectionArray = [["inspection_Session":"Current Inspection"], ["inspection_Session":"Past Inspection"], ["inspection_Session":"Future Inspection"]]
    
        // initialize hidden array so that the first is false and the rest true
        hidden = Array<Bool>(repeating: true, count: InspectionSectionArray.count)
        hidden[0] = false
    
        tableViewSecond.delegate = self
        tableViewSecond.dataSource = self
    }
    
    // and change numberOfSections to this
    func numberOfSections(in tableView: UITableView) -> Int {
        return InspectionSectionArray.count
    }
    

    【讨论】:

    • Bro 声明隐藏时显示错误,例如“Instance member 'InspectionSectionArray' cannot be used on type 'SecondViewController'”。
    【解决方案2】:

    只需更改此功能

    func tapFunction(sender: UITapGestureRecognizer?) {
    
        var section = 0
    
        if sender != nil {
            section = sender!.view!.tag
        }
    
        let indexPaths = (0..<InspectionArray.count).map { i in return IndexPath(item: i, section: section)  }
        hidden[section] = !hidden[section]
        tableViewSecond.beginUpdates()
        if hidden[section] {
            tableViewSecond.deleteRows(at: indexPaths, with: .fade)
        } else {
            tableViewSecond.insertRows(at: indexPaths, with: .fade)
        }
        tableViewSecond.endUpdates()
    }
    

    然后在viewdidLoad 中调用self.tapFunction(sender: nil)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      相关资源
      最近更新 更多