我有一个UITableView,它有多个Prototype单元格(具有不同的标识符),我有一个单独的类我的单元格!这是我创建我的TableViewController的方式: -
import UIKit class PostTableViewController: UITableViewController, UITextFieldDelegate { var postArray = [["Sean Paul","Got To Love You"],["California","21 January 2018"],["Martin Garrix"]] override func viewDidLoad() { super.viewDidLoad() tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 44 } override func viewDidAppear(animated: Bool) { } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 3 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows print(postArray[section].count) return postArray[section].count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = UITableViewCell() if indexPath.section == 0 && indexPath.row == 0{ cell = tableView.dequeueReusableCellWithIdentifier("titleCell", forIndexPath: indexPath) as! MultiLineTextInputTableViewCell } if indexPath.section == 0 && indexPath.row == 1 { cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MultiLineTextInputTableViewCell } if indexPath.section == 1 && indexPath.row == 0 { cell = tableView.dequeueReusableCellWithIdentifier("locationCell", forIndexPath: indexPath) as! MultiLineTextInputTableViewCell } if indexPath.section == 1 && indexPath.row == 1 { cell = tableView.dequeueReusableCellWithIdentifier("timeCell", forIndexPath: indexPath) as! MultiLineTextInputTableViewCell } if indexPath.section == 2 && indexPath.row == 0 { cell = tableView.dequeueReusableCellWithIdentifier("recipientCell", forIndexPath: indexPath) as! MultiLineTextInputTableViewCell } return cell } @IBAction func btnActionPost(sender: AnyObject) { let indexPath = NSIndexPath(forRow: 1, inSection: 0) print( tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text) // here i tried to get the text of first cell but text is not in the cell's label it is inside the TextView which is inside UItableViewCell tableView.cellForRowAtIndexPath(indexPath) } }
这就是我创建TableViewCellController的方式: -
import UIKit class MultiLineTextInputTableViewCell: UITableViewCell { // @IBOutlet weak var titleLabel: UILabel? @IBOutlet var textView: UITextView? @IBOutlet var titleTxtField: UITextField! @IBOutlet var locationTxtField: UITextField! @IBOutlet var timeTxtField: UITextField! @IBOutlet var recipientTxtField: UITextField! override init(style: UITableViewCellStyle, reuseIdentifier: String!) { super.init(style: style, reuseIdentifier: reuseIdentifier) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } /// Custom setter so we can initialise the height of the text view var textString: String { get { return textView?.text ?? "" } set { if let textView = textView { textView.text = newValue textViewDidChange(textView) } } } override func awakeFromNib() { super.awakeFromNib() let indexPath = NSIndexPath(forRow: 1, inSection: 0) // Disable scrolling inside the text view so we enlarge to fitted size textView?.scrollEnabled = false textView?.delegate = self } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) if selected { textView?.becomeFirstResponder() } else { textView?.resignFirstResponder() } } } extension MultiLineTextInputTableViewCell: UITextViewDelegate { func textViewDidChange(textView: UITextView) { let size = textView.bounds.size let newSize = textView.sizeThatFits(CGSize(width: size.width, height: CGFloat.max)) // Resize the cell only when cell's size is changed if size.height != newSize.height { UIView.setAnimationsEnabled(false) tableView?.beginUpdates() tableView?.endUpdates() UIView.setAnimationsEnabled(true) if let thisIndexPath = tableView?.indexPathForCell(self) { tableView?.scrollToRowAtIndexPath(thisIndexPath, atScrollPosition: .Bottom, animated: false) } } } } extension UITableViewCell { /// Search up the view hierarchy of the table view cell to find the containing table view var tableView: UITableView? { get { var table: UIView? = superview while !(table is UITableView) && table != nil { table = table?.superview } return table as? UITableView } } }
我尝试这个从单元格中获取文本: - 但它不是正确的方法
let indexPath = NSIndexPath(forRow: 1, inSection: 0) print( tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text) // here i tried to get the text of first cell but text is not in the cell's label it is inside the TextView which is inside UItableViewCell tableView.cellForRowAtIndexPath(indexPath)
如果有人知道那么请指导我这对我很有帮助:)
cellForRowAtIndexPath(_:)
返回一个UITableViewCell?
具有textLabel
属性的属性,它本身就是一个UILabel - 但是你永远不会在UITableViewCell上为textLabel分配任何东西; 您分配给您的自定义textView
属性.
试试这个:
let indexPath = NSIndexPath(forRow: 1, inSection: 0) let multilineCell = tableView.cellForRowAtIndexPath(indexPath) as? MultiLineTextInputTableViewCell // we cast here so that you can access your custom property. print(multilineCell?.textView.text)
这样您就可以在自定义单元格的TextView上进行操作.