2016-10-18 6 views
2

테이블 뷰 셀 자동 크기 조정 셀을 클릭하면 크기가 자동으로 셀 안의 내용에 맞도록 크기가 조정되므로 테이블 뷰를 작동 시키려고합니다. (텍스트의 다른 금액이 자동으로 다른 크기를 다시 것입니다.이 이미지 그래서셀 내용 (SWIFT)

Cell when not clicked (first state and size it should go back to when clicked = 44.0, 이것은 각 셀이 선택 해제만을 기본으로하고있을 것이다 단계입니다. 흰색 텍스트를 볼 수있는 유일한 일이 될 것 .

Cell clicked state. resizes to my "let selectedCellHeight: CGFloat = 88.0 " constant. Which needs to automatically size to fit the green text no matter how much is in there.

이가있는 tableview와의 ViewController에 대한 내 전체 코드입니다.

import UIKit 

class TasksViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet weak var tblTasks: UITableView! 

//For persisting data 
let defaults = UserDefaults.standard 


override func viewDidLoad() { 
    super.viewDidLoad() 



    self.tblTasks.backgroundColor = UIColor(red: 64/255.0, green: 67/255.0, blue: 68/255.0, alpha: 0) 


    self.tblTasks.reloadData() 
    self.tblTasks.register(UINib(nibName: "WhiteTaskTableViewCell", bundle: nil), forCellReuseIdentifier: "nameCell") 
    tblTasks.tableFooterView = UIView() 

    let darkModeColor = UIColor(red: 52/255.0, green: 55/255.0, blue: 55/255.0, alpha: 1.0) 
    view.backgroundColor = darkModeColor 


    tblTasks.dataSource = self; 

    // Do any additional setup after loading the view. 
} 



override func viewWillAppear(_ animated: Bool) { 
    self.tblTasks.reloadData() 
} 



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


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 
    return taskMgr.tasks.count 

} 



//Define how our cells look - 2 lines a heading and a subtitle 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 


    let identifier = "nameCell" 
    var cell: WhiteTaskTableViewCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? WhiteTaskTableViewCell 

    if cell == nil { 
     tableView.register(UINib(nibName: "WhiteTaskTableViewCell", bundle: nil), forCellReuseIdentifier: identifier) 
     cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? WhiteTaskTableViewCell 
    } 


    //  Assign the contents of our var "items" to the textLabel of each cell 
    //  cell.textLabel!.text = taskMgr.tasks[indexPath.row].name 
    //  cell.detailTextLabel!.text = taskMgr.tasks[indexPath.row].desc 

    cell.TaskNameLabel.text = taskMgr.tasks[indexPath.row].name 
    cell.NotesLabel.text = taskMgr.tasks[indexPath.row].note 

    cell.selectionStyle = .none 



    return cell 

} 



func numberOfSections(in tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
} 



    func tableView(_ willDisplayforRowAttableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    cell.backgroundColor = UIColor(red: 64/255.0, green: 67/255.0, blue: 68/255.0, alpha: 0) 
} 



func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if (editingStyle == UITableViewCellEditingStyle.delete) { 
     // handle delete (by removing the data from your array and updating the tableview) 

     taskMgr.removeTask(indexPath.row) 
     tblTasks.reloadData() 
    } 
} 



    // EXPAND CELL ON CLICK 


// Global Variables/Constants 

var selectedCellIndexPath: NSIndexPath? 

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

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

    self.tblTasks.rowHeight = UITableViewAutomaticDimension 

    if selectedCellIndexPath == indexPath as NSIndexPath? { 
     return selectedCellHeight 
    } 
    return unselectedCellHeight 
} 



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath as NSIndexPath? { 
     selectedCellIndexPath = nil 
    } else { 
     selectedCellIndexPath = indexPath as NSIndexPath? 
    } 

    tableView.beginUpdates() 
    tableView.endUpdates() 

    if selectedCellIndexPath != nil { 
     // This ensures, that the cell is fully visible once expanded 
     tableView.scrollToRow(at: indexPath as IndexPath, at: .none, animated: true) 
    } 
} 

/* 
// MARK: - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
} 
*/ 


} 
,536,913,632 10

저는 어떤 도움이나 통찰력도 열려 있습니다. 고맙습니다.

답변

0

콘센트를 UITableView으로 설정하고 rowHeight 매개 변수를 UITableViewAutomaticDimension으로 설정해야합니다. 또한 estimatedRowHeight을 필요에 맞는 값으로 설정해야합니다. 이렇게하면 셀의 크기가 내용의 크기에 맞춰집니다.

@IBOutlet weak var tableView: UITableView! 

override func viewDidLoad() { 
     super.viewDidLoad()   
     tableView.estimatedRowHeight = 55 
     tableView.rowHeight = UITableViewAutomaticDimension 
} 

이렇게하려면 셀의 모든 요소에 대해 자동 레이아웃 제약 조건을 설정해야합니다.

Source Apple Doc :

enter image description here

자동 크기 조정 테이블보기 세포

아이폰 OS에서 작업, 당신은 테이블보기 셀의 높이를 정의하는 자동 레이아웃을 사용할 수 있습니다; 그러나이 기능은 기본적으로 활성화되어 있지 않습니다.

일반적으로 셀의 높이는 테이블 뷰 대리자의 tableView:heightForRowAtIndexPath: 메서드에 의해 결정됩니다. 자체 크기 조정 테이블 셀을 사용하려면 테이블보기의 rowHeight 속성을 UITableViewAutomaticDimension으로 설정해야합니다. estimatedRowHeight 속성에도 값을 할당해야합니다. 이 두 속성이 모두 으로 설정되면 시스템은 자동 레이아웃을 사용하여 행의 실제 높이를 계산합니다.

또한 예상 행 높이를 만큼 정확하게 만들도록하십시오. 시스템은 이러한 예상을 기반으로 스크롤 막대 높이 과 같은 항목을 계산합니다. 견적이 정확할수록 더 많은 사용자 경험이 더 원활 해집니다.

enter image description here

+0

그것은했다! 무리 감사! 나는 이제 그 개념을 이해한다! – Nate

+0

당신은 반갑습니다. –