首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可扩展的表视图与sectionHeader配合使用

可扩展的表视图与sectionHeader配合使用
EN

Stack Overflow用户
提问于 2019-12-26 17:47:45
回答 2查看 103关注 0票数 0

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

对于Header XXX1 ->两个子菜单a和b.查看我的代码

代码语言:javascript
复制
 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

EN

回答 2

Stack Overflow用户

发布于 2019-12-26 18:47:02

对不起,这个教程很差。

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

代码语言:javascript
复制
class Section<T> {
    var name : String
    var items = [T]()

    var isCollapsed = false

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

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

代码语言:javascript
复制
struct Item {
    let title : String
    let selectorClosure : (() -> Void)?
}

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

代码语言:javascript
复制
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返回适当数量的项目

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

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

代码语言:javascript
复制
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

代码语言:javascript
复制
let currentSection = sections[section]
currentSection.isCollapsed.toggle()

并执行动画

titleForHeaderInSection也简单得多

代码语言:javascript
复制
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
   return sections[section].name
}

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

代码语言:javascript
复制
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: false)
    let item = sections[indexPath.section].items[indexPath.row]
    item.selectorClosure?()
}
票数 1
EN

Stack Overflow用户

发布于 2019-12-26 21:09:35

Swift4我想这会对你有帮助

代码语言:javascript
复制
// 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”;
        }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59486603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档