2017-12-27 25 views
3

私は動的に行の高さを調整することに1つの問題に直面しています。私はこの方法を使用することによって、我々はUITableview自動調整行問題Swift IOS?

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

を達成することができます知っているが、私の場合は私がUITableViewCellためXIBファイルを使用していますが、私はセルに影を追加しています。この場合、行の高さを動的に追加する方法。同時に、サーバーの時間に基づいて、私はボタンを表示したり隠したりしています。ですから誰も私にこの問題を解決する方法を教えてください。これは行インデックスメソッドのための私のセルです。

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 
    } 

答えて

0

私は制約値を設定して問題を解決しました。私はラベルのボトム拘束を「ボトムスペースからコンテナへ」と取った。これは、表示され、隠されたボタンを処理するための私のコードです。しかし、私はそれに応じて行の高さを修正することができませんでした。しかし何とか私は設計上の問題を修正しています。

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 

     } 
0

だから私はここでの問題は、細胞が44ポイントの見積高さにとどまるということであると仮定し

0

を構築し、実行し、その後のオートレイアウトのcontraintsして、ラベルの枠を設定してください。あなたが持っている問題は、セルのサブビューが変わっても、セルフレームのサイズそのものに関係するものは何もないということです。たとえば、whiteRoundedViewを特定のサイズにすると、セル自体のサイズにどのように影響しますか?

この状況に対処するには、2つの基本的な方法があります。

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 
} 

このようにして1回だけ追加されます。

実際にXIBを使用している場合は、シャドービューを設定してからパラメータを調整するだけです。

+0

まずはお返事ありがとうございます。あなたの提案を試してみましょう。 – Ram

+0

たとえば、ラベルやボタンのuitableviewcellバックグラウンドビューの内部に1つのUIを表示しています。私はここまでそのビューのための影を設定します。しかし、私の場合は、ボタンが時々表示され、時にはその行の高さを調整する時間を非表示にする – Ram

+0

私の答えで言うように、メソッド1を使用して、デリゲートで直接行の高さを指定するか、ビューをセルに結びつけて高さを決定できるようにします。 –

関連する問題