2014-07-23 2 views
8

클릭하면 내 tableView의 단일 행의 크기를 조정해야합니다. 내가 어떻게 할 수 있니? 아무도 나를 도울 수 없습니까?클릭하면 단일 셀의 높이를 변경하는 방법은 무엇입니까?

내보기 컨트롤러 클래스 :

class DayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet var daysWorkPointTable: UITableView 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     var nipName = UINib(nibName: "daysWorkPointsCell", bundle: nil) 

     self.daysWorkPointTable.registerNib(nipName, forCellReuseIdentifier: "daysWorkCell") 
    } 

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

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

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { 
     var cell = tableView.dequeueReusableCellWithIdentifier("daysWorkCell", forIndexPath: indexPath) as daysWorkPointsCell 

     return cell 
    } 

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 

    } 
} 

답변

31

먼저이 속성에 현재 선택된 셀의 indexPath 추적 할 수있다 : 당신이 할 수 있기 때문에, 그것은 선택해야

var selectedCellIndexPath: NSIndexPath? 

셀을 선택하지 않았습니다. 당신이 확인해야 이제

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if selectedCellIndexPath == indexPath { 
     return selectedCellHeight 
    } 
    return unselectedCellHeight 
} 

당신의 tableView(_:, didSelectRowAtIndexPath:) 방법 : 당신이 tableView(_:, heightForRowAtIndexPath:)을 구현해야 이제

let selectedCellHeight: CGFloat = 88.0 
let unselectedCellHeight: CGFloat = 44.0 

: 다음을 선택하고 선택되지 않은 상태에 대한 높이를 선언 (당신이 원하는에 값을 변경) 할 수 있습니다 선택된 로우 또는 비 선택 행 아니던 탭되었습니다

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

    tableView.beginUpdates() 
    tableView.endUpdates() 

    if selectedCellIndexPath != nil { 
     // This ensures, that the cell is fully visible once expanded 
     tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .None, animated: true) 
    } 
} 

beginUpdates() 및 012,호출은 애니메이션 높이 변경을 제공합니다.

높이 변경 애니메이션의 지속 시간을 변경하려면 애니메이션 블록 UIView.animationWithDuration(...)에서 beginUpdates()endUpdates() 호출을 래핑하고 원하는 값으로 설정할 수 있습니다.

this sample project에서이 코드가 실제로 작동하는지 확인할 수 있습니다.

+0

아아, 그냥 하나의 셀에서만 작동하도록 읽으십시오. 내 구현은 약간의 잔인한 행위이다. 왜냐하면 모든 셀에 대해 작동하기 때문이다. –

+0

let이 선언 된 경우 유효성 검사에서 selectedCellIndexPath 원인에 값을 할당 할 수 없으므로 var에 let을 변경하려고 시도했지만 NSIndexPath에 nil을 할당 할 수 없으므로 계속 오류를 발생시킵니다. 나 좀 도와 줄 수있어? 내가 도대체 ​​뭘 잘못하고있는 겁니까? –

+0

명시 적으로 self.selectedCellIndexPath에 할당하도록 할당을 변경했습니다. 이제는 잘될 것입니다.). –

관련 문제