代码之家  ›  专栏  ›  技术社区  ›  Ram

UITableview自动调整行问题Swift IOS?

  •  3
  • Ram  · 技术社区  · 7 年前

    我面临一个调整问题 UITableView 行高动态。我知道通过使用这种方法我们可以实现

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44.0; 
    

    但在我的情况下,我正在使用 XIB 的文件 UITableViewCell 我正在为细胞添加阴影。在这种情况下,如何动态添加行高。同时,根据服务器时间显示和隐藏按钮。所以,请任何人都可以建议我如何解决这个问题。这是我的行索引方法的单元格。

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let identifier = "Custom"
            var cell: PApplyLeaveTableViewCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? PApplyLeaveTableViewCell
            if cell == nil {
                tableView.register(UINib(nibName: "PApplyLeaveTableViewCell", bundle: nil), forCellReuseIdentifier: identifier)
                cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? PApplyLeaveTableViewCell
            }
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            cell.contentView.backgroundColor = UIColor.clear
            var localDic :NSDictionary!
            localDic = totlLeavesArray.object(at: indexPath.row) as! NSDictionary
            cell.acknowled_lbl.text = localDic["acknowledgement"] as? String
            cell.date_lbl.text = localDic["totalDate"] as? String
            cell.reason_lbl.text = localDic["reason"] as? String
            let compareDate = localDic["compare"] as? String
            if(compareDate == "No")
            {
                cell.delet_Btn.isHidden = true
                cell.edit_Btn.isHidden = true
            }
            else
            {
                cell.delet_Btn.isHidden = false
                cell.edit_Btn.isHidden = false
            }
            cell.edit_Btn.tag = indexPath.row
            cell.edit_Btn.addTarget(self, action: #selector(PApplyLeaveViewController.EditViewAction(_:)), for:.touchUpInside)
            cell.delet_Btn.tag = indexPath.row
    
            cell.delet_Btn.addTarget(self, action: #selector(PApplyLeaveViewController.DeleteAction(_:)), for:.touchUpInside)
            let whiteRoundedView : UIView = UIView(frame: CGRect(x: 5, y: 8, width: self.view.frame.size.width - 15, height: 220))
            whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])
            whiteRoundedView.layer.masksToBounds = false
            whiteRoundedView.layer.cornerRadius = 2.0
            whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
            whiteRoundedView.layer.shadowOpacity = 0.2
            cell.contentView.addSubview(whiteRoundedView)
            cell.contentView.sendSubview(toBack: whiteRoundedView)
            cell.contentView.backgroundColor = UIColor.clear
            return cell
        } 
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   Kathiresan Murugan    7 年前

    通过自动布局控件设置标签框架,然后生成并运行

        2
  •  0
  •   Upholder Of Truth Yawar    7 年前

    所以我假设这里的问题是细胞保持在他们估计的44个点的高度。问题是,尽管单元的子视图可能会更改,但与单元帧大小本身无关。例如,您将whiteRoundedView设置为特定大小,但这将如何影响单元格本身的大小。

    处理这种情况有两种基本方法:

    1) 不使用单元格的动态高度,而是使用此方法根据应用程序状态确定每个单元格的高度

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

    2) 切换到使用自动布局和设置约束将单元格大小与其内容关联。

    还有一点最终会对您产生影响,那就是每次系统需要单元格时,您都会添加一个whiteRoundedView,其中包括现有单元格的出列时间。已退出队列的单元格在创建时已经添加了whiteRoundedView。您确实希望将代码移动到创建单元格的位置,如下所示:

    if cell == nil {
        tableView.register(UINib(nibName: "PApplyLeaveTableViewCell", bundle: nil), forCellReuseIdentifier: identifier)
        cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? PApplyLeaveTableViewCell
    
        let whiteRoundedView : UIView = UIView(frame: CGRect(x: 5, y: 8, width: self.view.frame.size.width - 15, height: 220))
        whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])
        whiteRoundedView.layer.masksToBounds = false
        whiteRoundedView.layer.cornerRadius = 2.0
        whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
        whiteRoundedView.layer.shadowOpacity = 0.2
        cell.contentView.addSubview(whiteRoundedView)
        cell.contentView.sendSubview(toBack: whiteRoundedView)
        cell.contentView.backgroundColor = UIColor.clear
    }
    

    因此,它只能添加一次。

    事实上,如果您使用的是XIB,为什么不在其中设置阴影视图,然后调整参数。

        3
  •  0
  •   Ram    7 年前

    我通过设置约束值来解决问题。我将标签底部约束设置为“底部空间到容器”。这是我处理显示和隐藏按钮的代码。但我无法相应地确定行高。但不知何故,我正在解决设计问题。

    if compareDateString == currentDateString
            {
                cell.delete_Btn.isHidden = true
                cell.edit_Btn.isHidden = true
                cell.bottomConstraint.constant = 30
            }
    
            else if compareDateString! < currentDateString
            {
                cell.delete_Btn.isHidden = true
                cell.edit_Btn.isHidden = true
              cell.bottomConstraint.constant = 30
            }
            else
            {
                cell.delete_Btn.isHidden = false
                cell.edit_Btn.isHidden = false
                cell.bottomConstraint.constant = 45
    
            }