swift - 我如何获取 UITableViewCell 内的 textView 的文本值

标签 swift uitableview

我有一个 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 上进行操作。

关于swift - 我如何获取 UITableViewCell 内的 textView 的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34600825/

相关文章:

ios - UITableViewCell 使用自定义 layoutSubviews() 出现在屏幕上后隐藏部分内容

ios - Swift:作为参数传递的字典始终为空

ios - 无法在 UITableViewController 中设置 TableView 的约束

iphone - 为小型固定大小的 UITableView 禁用单元格重用

ios - UITableView:向上滑动时收缩标签栏和导航栏

带索引的 Swift 访问数组给出以下错误。知道为什么吗?

swift - 阻止大标题导航栏折叠

swift - UITableViewController 和预取

ios - UITableView 在 textFieldDidBeginEditing : 中获取 indexPath

json - 确定用于简单时间跟踪应用程序的数据模型