【问题标题】:Why cell is display overflow the table view in Swift为什么单元格在 Swift 中显示溢出表格视图
【发布时间】:2022-01-21 08:47:57
【问题描述】:

我试图在几天内找到解决方案。但是,网上的答案都不适合我的情况。 我已经修复了在表格视图中将单元格设置为 cliptobounds,并且还分配了约束来修复表格的位置和高度。 以下是我的部分代码。

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

我的代码和别人的不同是

  1. 这是一个可消耗的视图单元格,高度将根据单元格是否被消耗而改变
  2. 我为两个表格使用了一个可重复使用的单元格。

希望能就如何解决这个问题获得一些想法。 提前致谢。

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    您可以通过为每个单元格添加标题来实现此目的,然后当您单击它时,使用打开的单元格重新加载表格视图,看看这个例子:

    数据模型

    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
        }
    }
    

    自定义标题女巫会监听事件:

    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 和自定义标题委托

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多