2016-08-16 2 views
0

UITableviewCell 높이를 늘리려는 경우 TableViewCell 버튼 클릭시 UILabel 내용에 따라 다릅니다. 검색해 보았지만 구속 조건에 대한 답을 찾을 수 있습니다.swift : Constraints를 사용하지 않고 UILabel에 대한 TableViewCell 높이를 높이십시오.

'Constarints'/ Autolayout을 사용하지 않고 동적으로 UITabelViewCell 높이를 높이고 싶습니다.

나는 코드 아래 사용하여 GET 라벨의 높이가 :

func getLabelHeight(lbl : UILabel) -> CGFloat 
    { 
     let constraint = CGSizeMake(lbl.frame.size.width, CGFloat.max) 
     var size = CGSize() 

     let context = NSStringDrawingContext(); 

     let boundingBox : CGSize 

     boundingBox = NSString(string: lbl.text!).boundingRectWithSize(constraint, 
                    options: NSStringDrawingOptions.UsesLineFragmentOrigin, 
                    attributes: [NSFontAttributeName: self], 
                    context: context).size 

     size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height)) 
     return size.height; 
    } 

을하지만 난 jQuery과이 코드를 사용할 수 없습니다. TableViewCell 높이를 증가시키기 위해 나는 아래의 코드를 작성했습니다 :

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 

에서

func btnReadMoreClick(sender: UIButton){ 
     let buttonTag = sender.tag 

     indexOfReadMoreButton = sender.tag 
     isReadMoreButtonTouched = true 

     tableVDetail.beginUpdates() 
     tableVDetail.reloadRowsAtIndexPaths([NSIndexPath(forItem: indexOfReadMoreButton, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic) 
     tableVDetail.endUpdates() 
    } 

설정하고 셀 높이를하지만 내 출력을 얻을 수없는이 사용. :(사전!

+0

정말로이 방법을 사용하려면이 함수를 사용하여 높이를 정적으로 계산하고 바운드 대상 텍스트로 직접 전달하십시오. 그러면 CellName.getLabelHeight ...를 호출 할 수 있습니다. ... – mbutan

+0

@mbutan은 PLZ를 설명 할 수 있습니까? 나를 사용하는 코드. 또는 다른 방법이 있다면 plz도 그렇게 말하십시오. –

답변

0

에서

덕분에 당신이 당신의 세포에 적절하게 제약을 설정하는 경우, 그것은 나머지 작업을 수행해야합니다.

이 자동으로 셀의 크기를 조정하고 높이를 기대 계산합니다 도우미입니다.

extension UITableViewCell { 
    func calcualteHeight(inTableview tableview: UITableView) -> CGFloat { 
     self.setNeedsUpdateConstraints() 
     self.updateConstraintsIfNeeded() 
     self.bounds = CGRectMake(0, 0, CGRectGetWidth(UIScreen.mainScreen().bounds), CGRectGetHeight(self.bounds)) 
     self.setNeedsLayout() 
     self.layoutIfNeeded() 

     let height = self.contentView.systemLayoutSizeFittingSize(self.bounds.size, withHorizontalFittingPriority: UILayoutPriorityFittingSizeLevel, verticalFittingPriority: UILayoutPriorityFittingSizeLevel).height 
     return height 
    } 
} 

의 TableView 위임에, 당신의 계산 셀을 가져오고 데이터를 입력합니다.

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     let cell = tableView.dequeueReusableCellWithIdentifier(self.viewModel.reuseIdentifier(atIndexPath: indexPath)) as! YourCell 
     cell.setupData(fillCellYourData) 
     return cell.calcualteHeight(inTableview: tableView) 
} 
+0

셀에 제약 조건을 사용하지 않고 이것을 wnt했습니다. –

관련 문제