1

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

点击图片网址

我搜索了很多但没有工作

听到我的代码添加了带有 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)
    }
}
4

3 回答 3

0
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()
        }


}
于 2020-01-21T17:17:00.043 回答
0

没有部分,你也可以实现这一点。去做这个,

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

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

这是方法之一。使用部分也可以做到这一点。

于 2020-01-21T12:15:22.917 回答
0
//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
    }
}
于 2020-01-21T13:28:47.290 回答