2016-10-18 214 views
2

我試圖讓我的tableview工作,所以當單元格被點擊時,它調整大小以適應單元格內的內容自動。 (不同數量的文本會自動。重新大小不同自動重新調整表格單元格的單元格內容(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. 
} 
*/ 


} 

我願意接受任何幫助和見解。非常感謝你。

回答

0

您需要將出口設置爲您的UITableView並將rowHeight參數設置爲UITableViewAutomaticDimension。您還需要將estimatedRowHeight設置爲符合您需要的值。這樣Cell的大小將適合其內容的大小。

@IBOutlet weak var tableView: UITableView! 

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

爲此,您需要爲您的單元的每個元素設置Autolayout Constraints。

Source Apple Doc

enter image description here

具有自調整大小表格視圖單元

在iOS系統中,您可以使用自動佈局來定義表視圖 細胞的高度;但是,該功能默認情況下未啓用。

通常,單元格的高度由表視圖委託的方法確定。要啓用自定尺寸表 視圖單元,必須將表視圖的rowHeight屬性設置爲 UITableViewAutomaticDimension。您還必須爲 estimatedRowHeight屬性分配一個值。只要這兩個屬性都設置爲 ,系統就會使用自動佈局來計算行的實際高度。

此外,儘量使估計的行高精確到 。系統根據這些估算值計算項目,例如滾動條高度 。估算越精確,用戶體驗就越無縫。

enter image description here

+0

它的工作!謝謝一堆!我現在瞭解這個概念! – Nate

+0

不客氣的朋友 –