简体   繁体   English

为什么单元格显示溢出 Swift 中的表格视图

[英]Why cell is display overflow the table view in Swift

I have try to find the solution for it a few days time.我试图在几天内找到解决方案。 However non of the answers online are suitable for my situation.但是,网上的答案都不适合我的情况。 I have fix make the cell to cliptobounds in the table view and also assign constraints to fix the table position and height.我已经修复了在表格视图中将单元格设置为 cliptobounds 并分配约束来修复表格 position 和高度。 Below are some part of my code.以下是我的代码的一部分。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    if hiddenRow.contains(indexPath.row) || hiddenRow2.contains(indexPath.row){
        rowHeight.append(300)
        return 300 //Expanded
    }
    else{
        rowHeight.append(120)
        return 120 //Not Expanded
    }
    
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "med_reusable_cell", for: indexPath as IndexPath) as! MedListTableViewCell
    
    cell.backgroundColor = TRANSPARENT
    cell.layer.cornerRadius = DEFAULT_CORNER_RADIUS
    
    active_table_height.constant = self.view.frame.size.height * 11/36
    expired_table_height.constant = self.view.frame.size.height * 11/36

cell overflow单元格溢出

The different between my code and others are我的代码和其他代码的不同之处在于

  1. This is an expendable view cell which the height will be change based whether the cell is expended这是一个可消耗的视图单元格,其高度将根据单元格是否被消耗而改变
  2. I use a reusable cell for two tables.我为两个表格使用了一个可重复使用的单元格。

Hope can get some ideas on how this could solve.希望能得到一些关于如何解决这个问题的想法。 Thanks in advanced.先谢谢了。

You can achieve this by adding a header to each cell, then when you'll click it, reload the table view with the opened cell look at this example:您可以通过向每个单元格添加 header 来实现此目的,然后当您单击它时,使用打开的单元格重新加载表格视图,看看这个例子:

DataModel :数据模型

struct  DataItem {
    var isExpand: Bool
    var title: String
    var value:String
    
    
    init(isExpand:Bool = false, title:String, value:String) {

        self.isExpand = isExpand
        self.title = title
        self.value = value
    }
}

Custom Header witch will listen to events:自定义 Header女巫将监听事件:

protocol CustomHeaderViewDelegate: AnyObject {
    func headerViewTap(_ section: Int)
}

class CustomHeaderView: UITableViewHeaderFooterView {
    
    weak var delegate: CustomHeaderViewDelegate?
    var sectionNumber: Int?
            
     required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        let gesture = UITapGestureRecognizer(target: self, action: #selector(CustomHeaderView.tableViewSectionTapped(_:)))
        self.addGestureRecognizer(gesture)
    }

    @objc func tableViewSectionTapped(_ gesture: UIGestureRecognizer) {
        if let sectionNumber =  sectionNumber{
            delegate?.headerViewTap(sectionNumber)
        }
    }
}

TableView and Custom Header delegates TableView 和自定义 Header 代表

extension ViewController :  UITableViewDelegate, UITableViewDataSource{
    //The number of sections fits the number of cells, the current list is an array of DataObject, holding a title and a content.
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.currentList.count
    }
    //Each section(group of cells) contains one row
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
        return cell
    }
    
    //update heights for row if the header has been taped
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let isExpanded = self.currentList[indexPath.section].isExpand
        if isExpanded {
            return UITableView.automaticDimension
        }
        return 0
    }
    //update the estimatedHeightForRowAt if the hader has been taped
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        let isExpanded = self.currentList[indexPath.section].isExpand
        if isExpanded{
            return UITableView.automaticDimension
        }
        return 0
    }
    
    //returns a custom header
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "Header") as! CustomHeaderView
        return headerView
    }
}

extension ViewController : CustomeHeaderViewDelegate{
    func headerViewTap(_ section: Int) {
        selectedItem = self.currentList[section]
        let output = self.currentList.map({ (item:DataItem) -> DataItem in
            var result = item
            if result.title == self.selectedItem?.title{
                result.isExpand = !result.isExpand
            }
            return result
        })
        self.currentList = output
        self.tableView.reloadSections(IndexSet(integer: section), with: UITableView.RowAnimation.automatic)
        self.tableView.endUpdates()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM