单击Swift 4中的自定义按钮时调整单元格大小

时间:2018-01-01 10:33:31

标签: ios swift

我有一个视图控制器,其中我使用了表视图控制器。在我的单元格中有一个按钮,当用户单击单元格大小时应该达到550并且当再次单击时它应该回到其原始高度。我在搜索之后尝试了一些代码,但它不起作用是他们可以为我工作的任何解决方案吗?我的代码是这个,

var indexOfCellToExpand: Int!

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  if indexPath.row == indexOfCellToExpand {
    return 170 + expandedLabel.frame.height - 38
  }
  return 170
}

2 个答案:

答案 0 :(得分:1)

Swift 4.x

public function getCustomFilterData(Request $request)
{
    $vendorTableName = with(new Vendors())->getTable();
    $categoryTableName = with(new Excategories())->getTable();
    $orders = Checks::select(["checks.*","vendors.vendor_name","excategories.category_name"]);

    if($request->has('start_amount') && $request->has('end_amount')) {
        $min = $request->start_amount;
        $max = $request->end_amount;

        $orders->Wherebetween('amount',[$min,$max]);
    }

    if($request->has('start_date') && $request->has('end_date')) {
        $arrStart = explode("/", Input::get('start_date'));
        $arrEnd = explode("/", Input::get('end_date'));
        $start = Carbon::create($arrStart[2], $arrStart[0], $arrStart[1], 0, 0, 0);
        $end = Carbon::create($arrEnd[2], $arrEnd[0], $arrEnd[1], 23, 59, 59);

        $orders->between($start, $end);
    }

    $orders = $orders->leftJoin($vendorTableName,$vendorTableName.".vendor_id","=","checks.vendor_id")
                    ->leftJoin($categoryTableName,$categoryTableName.".category_id","=","checks.category_id")
                    ->get();

    return Datatables::of($orders)->make( TRUE );
}

答案 1 :(得分:1)

对展开折叠单元格使用自动布局, 附加该案例的演示

link:https://www.dropbox.com/s/ieltq0honml35l8/TAbleDemo.zip?dl=0.

设置单元格按钮高度约束并更新select-deselect事件上的值约束,只需重新加载数据

主视图控制器代码中的

self.tblView.estimatedRowHeight = 100
self.tblView.rowHeight = UITableViewAutomaticDimension

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

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

        cell.btnExpandCollepse.addTarget(self, action: #selector(ViewController.expandCollapse(sender:)), for: .touchUpInside)
        return cell

    }

 @objc func expandCollapse(sender:UIButton) {

        self.tblView.reloadData()
    }

Cell Class中的代码

@IBOutlet var btnExpandCollepse: UIButton!
@IBOutlet var constraintBtnHeight: NSLayoutConstraint!

@IBAction func onExpandCollepse(_ sender: UIButton) {

        sender.isSelected = !sender.isSelected

        if !sender.isSelected{

            self.constraintBtnHeight.constant = 50
        }else{
            self.constraintBtnHeight.constant = 500
        }
    }

用于下图的约束检查 http://prntscr.com/hur8ym

更新了自定义单元格的Lable自定义内容高度的演示 https://www.dropbox.com/s/o742kflg5yeofb8/TAbleDemo%202.zip?dl=0

https://www.dropbox.com/s/o742kflg5yeofb8/TAbleDemo%202.zip?dl=0