ios - 테이블뷰 - uitableview example swift 4




테이블 뷰 셀 확장 및 축소 (2)

확장 및 셀 축소 할 수 있지만 단추 제목을 변경하려면 UITableViewCell 내부 함수 (확장 및 축소) 호출하고 싶습니다.

import UIKit

class MyTicketsTableViewController: UITableViewController {

    var selectedIndexPath: NSIndexPath?
    var extraHeight: CGFloat = 100

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTicketsTableViewCell
        return cell
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if(selectedIndexPath != nil && indexPath.compare(selectedIndexPath!) == NSComparisonResult.OrderedSame) {
            return 230 + extraHeight
        }

        return 230.0
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if(selectedIndexPath == indexPath) {
            selectedIndexPath = nil
        } else {
            selectedIndexPath = indexPath
        }

        tableView.beginUpdates()
        tableView.endUpdates()
    }
}

import UIKit

class MyTicketsTableViewCell: UITableViewCell {

    @IBOutlet weak var expandButton: ExpandButton!
    @IBOutlet weak var detailsHeightConstraint: NSLayoutConstraint!

    var defaultHeight: CGFloat!

    override func awakeFromNib() {
        super.awakeFromNib()

        defaultHeight = detailsHeightConstraint.constant

        expandButton.button.setTitle("TAP FOR DETAILS", forState: .Normal)
        detailsHeightConstraint.constant = 30
    }

    func expand() {
        UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveLinear, animations: {
            self.expandButton.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 0.99))
            self.detailsHeightConstraint.constant = self.defaultHeight
            self.layoutIfNeeded()

            }, completion: { finished in
                self.expandButton.button.setTitle("CLOSE", forState: .Normal)
        })
    }

    func collapse() {
        UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveLinear, animations: {
            self.expandButton.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 0.0))
            self.detailsHeightConstraint.constant = CGFloat(30.0)
            self.layoutIfNeeded()

            }, completion: { finished in
                self.expandButton.button.setTitle("TAP FOR DETAILS", forState: .Normal)
        })
    }

}


필요한 것은 UITableView Delegate를 다음과 같이 구현하는 것입니다.

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

기본적으로 estimatedHeight는 CGRectZero입니다. 값을 설정하면 자동 크기 조정이 가능합니다 (UICollectionView와 동일한 상황). 다음과 같이 할 수도 있습니다.

tableView?.estimatedRowHeight = CGSizeMake(50.f, 50.f); 

그런 다음 셀 내부에서 제약 조건을 설정해야합니다.

이 게시물을 확인하십시오 : https://www.hackingwithswift.com/read/32/2/automatically-resizing-uitableviewcells-with-dynamic-type-and-ns

희망이 도움이)


MyTicketsTableViewController 클래스에서 cellForRowAtIndexPath 데이터 소스 메서드 내에서 버튼의 대상을 추가합니다.

cell.expandButton.addTarget(self, action: "expandorcollapsed:", forControlEvents: UIControlEvents.TouchUpInside)




uitableview