【问题标题】:Tableview header cell expand with same cardView in swift?Tableview标题单元格在swift中使用相同的cardView扩展?
【发布时间】:2020-01-21 12:00:44
【问题描述】:

我已经附上了图片点击卡片视图动态展开表格单元格内的同一张卡片它可以实现这个吗?

我搜索了很多但没有工作

听到我的代码在 CardView 中添加了标题单元格 添加箭头按钮以单击按钮展开单元格 它能够扩展但不能在父卡中显示差异卡

我已经添加了我的源代码

 var hiddenSections = Set<Int>()
       let tableViewData = [
           ["1","2","3","4","5"],
           ["1","2","3","4","5"],
           ["1","2","3","4","5"],
       ]
    override func viewDidLoad() {
        super.viewDidLoad()

     let CustomeHeaderNib = UINib(nibName: "CustomSectionHeader", bundle: Bundle.main)
            historyTableView.register(CustomeHeaderNib, forHeaderFooterViewReuseIdentifier: "customSectionHeader")
}

func numberOfSections(in tableView: UITableView) -> Int {
    return self.tableViewData.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.hiddenSections.contains(section) {
        return 0
    }
    return self.tableViewData[section].count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->    UITableViewCell {
    let cell = UITableViewCell()
    cell.textLabel?.text = self.tableViewData[indexPath.section][indexPath.row]        
    return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return view.frame.width/4
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = self.historyTableView.dequeueReusableHeaderFooterView(withIdentifier: "customSectionHeader") as! CustomSectionHeader
        header.setupCornerRadious()
        let sectionButton = header.expandBtn
        sectionButton?.setTitle(String(section),
                               for: .normal)
        sectionButton?.tag = section
        sectionButton?.addTarget(self,action: #selector(self.hideSection(sender:)), for: .touchUpInside)
        return header
    }
     @objc
private func hideSection(sender: UIButton) {
    let section = sender.tag

    func indexPathsForSection() -> [IndexPath] {
        var indexPaths = [IndexPath]()

        for row in 0..<self.tableViewData[section].count {
            indexPaths.append(IndexPath(row: row,
                                        section: section))
        }

        return indexPaths
    }

    if self.hiddenSections.contains(section) {
        self.hiddenSections.remove(section)
        self.historyTableView.insertRows(at: indexPathsForSection(),
                                  with: .fade)
    } else {
        self.hiddenSections.insert(section)
        self.historyTableView.deleteRows(at: indexPathsForSection(),
                                  with: .fade)
    }
}

【问题讨论】:

  • 您找到解决方案了吗?我也在找同样的东西,你能帮帮我吗?

标签: ios objective-c swift uitableview


【解决方案1】:

使用 out 部分也可以实现此目的。为此,

1.返回单元格高度作为截面高度。如果用户单击单元格,则将总内容高度返回到特定单元格。

2.您需要一个数组,如果用户选择单元格,则将 indexPath 编号添加到数组中。如果选择已经展开单元格,则将其从数组中删除。在索引处检查 indexPath 的行高是否在数组中。

这是其中一种方式。您也可以使用部分来做到这一点。

【讨论】:

  • 怎么做,有样品可以分享一下吗?
  • 我已经这样做了。我无法共享该代码。但我会向您发送示例代码。给我一些时间我会发送它。
【解决方案2】:
//MARK:- UITableView Related Methods
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrDict.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    //        var cel = tblExpandedTest.dequeueReusableCellWithIdentifier("expCell", forIndexPath: indexPath) as! CDTableViewCell

    var cel : CaseHearingTabTVC! = tableView.dequeueReusableCell(withIdentifier: "caseHearingTabCell") as! CaseHearingTabTVC

    if(cel == nil)
    {
        cel = Bundle.main.loadNibNamed("caseHearingTabCell", owner: self, options: nil)?[0] as! CaseHearingTabTVC;
    }

    //cell?.backgroundColor = UIColor.white
    cel.delegate = self


    if indexPath != selctedIndexPath{
        cel.subview_desc.isHidden = true
        cel.subview_remarks.isHidden = true
        cel.lblHearingTime.isHidden = true
    }
    else {
        cel.subview_desc.isHidden = false
        cel.subview_remarks.isHidden = false
        cel.lblHearingTime.isHidden = false
    }
    return cel

}

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

    selectIndex = true;

    if(selectedInd == indexPath.row) {
        selectedInd = -1
    }
    else{
        let currentCell = tableView.cellForRow(at: indexPath)! as! CaseHearingTabTVC
        cellUpdatedHeight = Float(currentCell.lblHearingTime.frame.origin.y + currentCell.lblHearingTime.frame.size.height) + 2;

        selectedInd = -1
        tblCaseHearing.reloadData()
        selectedInd = indexPath.row
    }

    let previousPth = selctedIndexPath

    if indexPath == selctedIndexPath{
        selctedIndexPath = nil
    }else{
        selctedIndexPath = indexPath
    }
    var indexPaths : Array<IndexPath> = []
    if let previous = previousPth{
        indexPaths = [previous]
    }
    if let current = selctedIndexPath{
        indexPaths = [current]
    }
    if indexPaths.count>0{
        tblCaseHearing.reloadRows(at: indexPaths, with: UITableView.RowAnimation.automatic)
    }
}

func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowIndexPath indexPath:IndexPath) {

    (cell as! CaseHearingTabTVC).watchFrameChanges()
}

func tableView(_ tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowIndexPath indexPath:IndexPath) {

    (cell as! CaseHearingTabTVC).ignoreFrameChanges()
}

func tableView(_ TableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
    if indexPath == selctedIndexPath{
        return CGFloat(cellUpdatedHeight)

    }else{
        return CaseHearingTabTVC.defaultHeight
    }
}

【讨论】:

    【解决方案3】:
    Best approach is to create two different cells for normal card and expanded card. 
    
    fileprivate var selectedIndex: Int?
    
    func registerTableViewCells() {
            tableView.register(UINib(nibName:Nib.CardCell , bundle: nil), forCellReuseIdentifier: "CardCell")
            tableView.register(UINib(nibName:Nib.ExpandedCardCell , bundle: nil), forCellReuseIdentifier: "ExpandedCardCell")
    
        }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.registerTableViewCells()
     }
    

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

        guard let index = selectedIndex else {
                return 115
            }
            if index == indexPath.row{
                return 200
            }
            return 115
        }
    

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

        if let selected = selectedIndex, selected == indexPath.row{
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "ExpandedCardCell", for: indexPath) as! ExpandedCardCell
            return cell
        }
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "CardCell", for: indexPath) as! CardCell
    
        return cell
    
    }
    

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

            if selectedIndex == indexPath.row{
                selectedIndex = nil
            }
            else{
                selectedIndex = indexPath.row
            }
            UIView.performWithoutAnimation {
                tableView.reloadData()
            }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      相关资源
      最近更新 更多