【问题标题】:Expandable Tableview work with sectionHeader可扩展的 Tableview 与 sectionHeader 一起使用
【发布时间】:2019-12-26 09:47:44
【问题描述】:

我正在尝试使用 Expandable Tableview 加载我的不同控制器,但我的 headerview 已设置 作为切换条件

对于 Header XXX1 -> 两个子菜单 a 和 b .. 对于标题 XXX2-> 子菜单 c 但是对于 Header XXX3 没有子菜单,所以我将使用 XXX3 进行点击(目前正在使用 check SectionData.count == 0 )但是对于多个如何管理..查看我的代码

 sectionNames = ["xxxx1","xxxx2","xxx3","xxxx4"] //this is main header
 sectionItems = [ ["a","b"],[c],[],[],[],[],[],[]]// This is sub menu items

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if (self.expandedSectionHeaderNumber == section) {
            let arrayOfItems = self.sectionItems[section] as! NSArray
            return arrayOfItems.count;
        } else {
            return 0;
        }
        //return arraylist.count
    }


    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if (self.sectionNames.count != 0) {
            return self.sectionNames[section] as? String
        }
        return ""
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 60.0;
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
        return footerView
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.5

    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifer, for: indexPath)
        let section = self.sectionItems[indexPath.section] as! NSArray
        cell.textLabel?.textColor = UIColor.black
        cell.textLabel?.text = section[indexPath.row] as? String
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if indexPath.row == 0 {

        }


        let indexPath = tableView.indexPathForSelectedRow
        //  print(indexPath as Any)

        //getting the current cell from the index path
        let currentCell = tableView.cellForRow(at: indexPath!)! as UITableViewCell
        //   print(currentCell as Any)

        //getting the text of that cell
        let currentItem = currentCell.textLabel!.text
        print(currentItem!)

        switch currentItem { 
          case "XXXX1":
            //// Here unable to do any work
            break
          case "a":
            APICalla()
          case "b":
            APICallb ()
          default:
            break
        }
        return
    }

Using this link

【问题讨论】:

  • 不明白。请说得更清楚..您想知道如何为不同的部分和行管理 didSelectRowAt 吗?
  • 在这里您可以看到我将在 (cellForRowAt) 处管理 sectionItems,但如何在 (didSelectRowAt) 处将 sectionNames 管理为我的切换条件

标签: ios swift uitableview


【解决方案1】:

对不起,这个教程很差。

Swift 是一种面向对象的语言,因此请使用自定义模型、带有 nameitems 的通用 Section 对象以及该部分是否折叠时的信息

class Section<T> {
    var name : String
    var items = [T]()

    var isCollapsed = false

    init(name : String, items : [T] = []) {
        self.name = name
        self.items = items
    }
}

以及在didSelect中调用的带有标题和闭包的项目的合适结构

struct Item {
    let title : String
    let selectorClosure : (() -> Void)?
}

而不是使用多个数组一致地填充数据源数组

var sections = [Section<Item>(name:"xxxx1", items: [Item(title: "a", selectorClosure: APICalla), Item(title: "b", selectorClosure: APICallb)]),
                Section<Item>(name:"xxxx2", items: [Item(title: "c", selectorClosure: APICallc)]),
                Section<Item>(name:"xxxx3")]

numberOfRowsInSection 中,根据isCollapsed 返回正确的项目数

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let currentSection = sections[section]
    return (currentSection.isCollapsed) ? 0 : currentSection.items.count
}

cellForRow 中不要使用无类型基础集合类型

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifer, for: indexPath)
    let item = sections[indexPath.section].items[indexPath.row]
    cell.textLabel?.textColor = UIColor.black
    cell.textLabel?.text = item.title
    return cell
}

在折叠/展开部分的方法中,只需切换isCollapsed

let currentSection = sections[section]
currentSection.isCollapsed.toggle()

并执行动画


titleForHeaderInSection 也简单得多

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
   return sections[section].name
}

didSelectRow 从不视图(单元格)获取任何数据从模型中获取(数据源数组)并调用选择器闭包。有了这个逻辑,就不需要开关了。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: false)
    let item = sections[indexPath.section].items[indexPath.row]
    item.selectorClosure?()
}

【讨论】:

  • '(ViewController) -> () -> ()' 不能转换为 '(() -> Void)?'... at ``` var section = [Section (name:"xxxx1", items: [Item(title: "a", selectorClosure: APICalla), Item(title: "b", selectorClosure: APICallb)]), ```
  • APICalla 在哪里以及如何声明?并且sections 必须在视图控制器中声明。
  • 它不是像 func APICallc() { alert("test") } 这样的调用的基本函数()
  • 和部分在视图控制器中声明
  • 那么代码应该可以工作。我测试了它。这些方法也必须在同一个视图控制器中声明。
【解决方案2】:

Swift4我认为这会对你有所帮助

// declare globally
   var isExpanded : Bool = true
   var indexOfSection = Int()
   var yourArray = [ModelName]()
 override func viewDidLoad() {
        super.viewDidLoad()
        indexOfSection = 999
    }
extension ViewController: UITableViewDelegate, UITableViewDataSource
{
    func numberOfSections(in tableView: UITableView) -> Int {
            if yourArray.count > 0{
                return yourArray.count
            }else{
                return 0

        }

    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView(frame: CGRect(x: view.frame.origin.x,y: 0 , width: view.frame.size.width ,height: 60))
        headerView.backgroundColor = .white
        let collapseBtn = UIButton(frame: CGRect(x: headerView.frame.origin.x,y: headerView.frame.origin.y , width: view.frame.size.width ,height: 60))
        collapseBtn.addTarget(self, action: #selector(expandSection(sender:)), for: .touchUpInside)
        collapseBtn.tag = section
        collapseBtn.backgroundColor = .clear
        headerView.addSubview(collapseBtn)

        return headerView
    }

    @objc func expandSection(sender:UIButton){
        print(sender.tag)
        if isExpanded == true{
            indexOfSection = sender.tag
            mIdeaTableView.reloadData()
            isExpanded = false
            mTableView.reloadSections([indexOfSection], with: UITableView.RowAnimation.bottom)
        }else{
            indexOfSection = 999
            isExpanded = true
            self.mTableView.reloadData()

        }
    }


    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 60

    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            if yourArray.count > 0{
                if yourArray[section].items!.count > 0{
                    if indexOfSection == section{
                        return yourArray[section].items!.count
                    }else{
                        return 0
                    }
                }else{
                    return 0
                }
            }else{
                return 0
            }

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCell(withIdentifier: “CellID”, for: indexPath) as! Cell
            if yourArray[indexPath.section]. items!.count > 0{
                if yourArray[indexPath.section]. items!.count > 0{
                    let ideas = yourArray[indexPath.section].ideaItems
                    if ideas!.count > 0{
                        if indexOfSection == indexPath.section{
                            cell.mLbl.text = ideas![indexPath.row].name ?? ""
                            if ideas![indexPath.row].isExpanded == true{
                                cell.mAddImg.image = #imageLiteral(resourceName: "tick")
                            }else{
                                cell.mAddImg.image = #imageLiteral(resourceName: "edit213-1")
                            }
                        }
                    }
                }


            }

            return cell

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
                return 60
    }
 }

//Structure of my response 
{
            items =             (
                                {
                    name = “a”;
                },
                                {
                    name = “b”;
                },
                                      );
            name = “xxxx1”;
        }
            items =             (
                                {
                    name = “c”;
                },
                                      );
            name = “xxxx2”;
        }

}

【讨论】:

    猜你喜欢
    • 2018-03-14
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2017-11-10
    相关资源
    最近更新 更多