【问题标题】:how to perform specific action at certain table view row in swift?如何快速在某些表格视图行执行特定操作?
【发布时间】:2018-05-24 08:12:56
【问题描述】:

我有一个表格视图单元格。我为公寓的租户制作了一个应用程序来报告房间设施的缺陷。如果缺陷已经修复(状态:完成),来自服务器的数据将给出defect.status == 2(如果defect.status == 1,仍然在待修复的过程中),它将显示YES和NO按钮,如上图。

我希望如果它仍在修复过程中,包含“您是否满意”标签和是否按钮的视图将不会出现。预期的结果应该如下图所示

这是我用来删除满意或不满意的代码

extension RequestDefectVC : UITableViewDataSource {

     //MARK: Table View Delegate & Datasource

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "defectCell", for: indexPath) as! RequestDefectCell

        let dataDefect = listDefects[indexPath.row]
        cell.defectData = dataDefect

        if dataDefect.status == 2 {
            if let wantToRemoveView = cell.commentResponseView {
                wantToRemoveView.removeFromSuperview()
            }
        }


        return cell
    }

}

但不幸的是,如果触发了wantToRemoveView.removeFromSuperview(),它将删除所有单元格中的所有视图,即使状态如下图所示完成

我希望satisfied or not view在状态完成时出现,否则将被删除。该怎么做?

【问题讨论】:

  • 为什么不创建两种不同的细胞类型,一种是另一种的子类?

标签: ios swift uitableview uitableviewrowaction


【解决方案1】:

由于您的装扮细胞被重复使用,删除视图会导致不确定的效果。您实际上不需要删除特定视图,只要它保持不可见。

if dataDefect.status == 2 {
    if let wantToRemoveView = cell.commentResponseView {
        wantToRemoveView.isHidden = true
    }
} else {
    if let wantToRemoveView = cell.commentResponseView {
        wantToRemoveView.isHidden = false
    }
}

【讨论】:

  • 简单的解决方案,太棒了!
【解决方案2】:

为该视图创建一个高度约束并将其挂钩为IBOutlet并根据cellForRowAt中的设置控制它的常量

self.askViewH.constant = show ? 50 : 0

cell.layoutIfNeeded()

return cell

我希望您使用自动 tableView 单元格

【讨论】:

    【解决方案3】:

    @Alexa289 一个建议是您可以采用 UIView 的 heightConstraints。然后创建您的高度约束的IBOutlet,并在您想要隐藏时将其设为常量 0,否则将值分配给您的静态高度。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "defectCell", for: indexPath) as! RequestDefectCell
    
        let dataDefect = listDefects[indexPath.row]
        cell.defectData = dataDefect
    
        if dataDefect.status == 2 {
            cell.viewSatisficationHeightConstraints.constant = 50
        } else {
            cell.viewSatisficationHeightConstraints.constant = 0
        }
    
    
        return cell
    }
    

    第二个建议是您可以在视图中使用标签和按钮并将stackview嵌入到您的视图中(视图包含标签和按钮)

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "defectCell", for: indexPath) as! RequestDefectCell
    
        let dataDefect = listDefects[indexPath.row]
        cell.defectData = dataDefect
    
        if dataDefect.status == 2 {
            cell.viewSatisfication.isHidden = false
        } else {
            cell.viewSatisfication.isHidden = true
        }
    
    
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
       return UITableViewAutomaticDimension
    }
    
    override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    
       return 40
    }
    

    【讨论】:

      【解决方案4】:

      您可以阅读有关UIStackView 的信息,这使隐藏事情变得更容易。如果您不使用 stackview 并隐藏内容,则 UI 将不会很好,因为隐藏视图使用的空间仍然存在。所以在需要隐藏或显示某些视图时最好使用 stackView。

      UIStackView:https://developer.apple.com/documentation/uikit/uistackview

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-25
        • 1970-01-01
        • 2015-11-13
        • 1970-01-01
        • 2017-06-08
        • 1970-01-01
        • 1970-01-01
        • 2020-04-27
        相关资源
        最近更新 更多