我今天花了六个多小时研究并尝试创建动态单元格但没有成功。我也找到了一些很棒的教程。我做错了什么,希望有人能告诉我什么。
我的应用程序: 表格开始为空。单击添加按钮会显示一个模态 VC,它有两个文本输入字段(标题/描述)。该数据被传递回表。主 VC 中的 unwind 方法重新加载表数据。
问题:如果单元格的描述部分超过一行,它就会被截断。
这是我的主视图控制器中的代码,其中包含表格:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Initial Setup
//***** ----- ***** ------ ***** ----- ***** ----- *****
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Automatic table reload upon unwind
@IBAction func unwindToMain(segue: UIStoryboardSegue) {
//RW's code for dynamic cell height
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80.0
tableView.reloadData()
}
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Functions
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Swipe to delete task cell
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if(editingStyle == UITableViewCellEditingStyle.Delete) {
taskMgr.tasks.removeAtIndex(indexPath.row)
tableView.reloadData()
}
}
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Table View & Cell Setup
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Tells the table how many rows it should render
//*Looks to the taskMgr to count tasks, creates equal # of rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskMgr.tasks.count
}
//Creates the individual cells. If the above function returns 3, this runs 3 times
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "BasicCell")
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80.0
cell.textLabel?.text = taskMgr.tasks[indexPath.row].name
cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].desc
return cell
}
}
我的模态/编辑器视图中的代码:
class EditorView: UIViewController, UITextFieldDelegate {
@IBOutlet var txtTask: UITextField!
@IBOutlet var txtDesc: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Keyboard Functionality
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Dismisses keyboard upon tapping the return key
func textFieldShouldReturn(textField: UITextField) -> Bool{
textField.resignFirstResponder()
return true
}
//Dismisses keyboard upon touch outside text boxes
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Code to run upon Editor being dismissed
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Actions performed upon tapping the 'Finish' button
@IBAction func dismissEditorView(sender: AnyObject) {
//Calls a previously written function to append textfield input to the table array
taskMgr.addTask(txtTask.text, desc: txtDesc.text)
//Dismisses the EditorView
dismissViewControllerAnimated(true, completion: nil)
}
}
我的任务管理器文件包含一个将新项目附加到表的功能:
import UIKit
//This has global scope!!
var taskMgr: TaskManager = TaskManager()
struct task {
var name = "Name TBD"
var desc = "Desc TBD"
}
class TaskManager: NSObject {
var tasks = [task]()
func addTask(name: String, desc: String){
tasks.append(task(name: name, desc: desc))
}
}
我一直参考的教程:
http://natashatherobot.com/ios-8-self-sizing-table-view-cells-with-dynamic-type/
http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift
http://coding.tabasoft.it/ios/ios8-self-sizing-uitableview-cells/
http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html
我认为我做对的事情:
- 表格添加到视图,原型单元格添加到表格
- 向单元格添加了两个标签。添加了自动布局。顶部标签约束是容器(前导、尾随和顶部)。底部约束是第二个标签。底部标签具有从上到下的标签,然后是前导、尾随和底部的容器。
- 顶部标签具有 751 级抗压缩和内容拥抱的优先级。底部标签对所有四个都具有 750 优先级。
- 两个标签的首选宽度都设置为自动。显式未选中