【问题标题】:Text entry in UITableview similar to "Tasks/Reminders" appUITableview 中的文本条目类似于“任务/提醒”应用程序
【发布时间】:2019-11-09 01:56:22
【问题描述】:

目标:创建类似于 iOS Tasks/Reminders/Schedule 应用的文本输入方法

我知道如何将UITextView 添加到动态 UITablviewCell 并使其可编辑、关闭滚动并使单元格根据输入的文本动态调整其高度。

我也使用textViewShouldEndEditing的方法来找出光标在哪里。

我正在努力并需要你们帮助的地方是弄清楚如何在用户按键盘输入时动态添加单元格,以便他们可以开始在下面的下一个单元格中输入。 另外,在加载应用程序时,我目前将numberOfRowsInSection 设置为 10,有没有办法不对值进行硬编码?

我还想将加号按钮添加到下一个要添加的单元格,就像在 iOS 应用程序中一样,如果您能以正确的方式指导我,那将非常棒。

提前谢谢大家

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if !isIngredients{
        return Method.count
    } else {
        return listItems.count
    }

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "AddMetaDetails", for: indexPath) as! tvcAddMetaDetails

    if isIngredients {
        cell.textLabel?.text = "\(indexPath.row + 1). \(listItems[indexPath.row])"
        return cell
    } else  {
        cell.textLabel?.text = "\(indexPath.row + 1). \(Method[indexPath.row])"
        return cell
    }

}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    if !islistItems {
        Method.append(txtAddMetaDetails.text!)
        txtAddMetaDetails.text = ""
        tvAddMetaDetails.reloadData()
    } else {
        listItems.append(txtAddMetaDetails.text!)
        txtAddMetaDetails.text = ""
        tvAddMetaDetails.reloadData()
    }
    return true
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    tvAddMetaDetails.estimatedRowHeight = 60
    tvAddMetaDetails.estimatedRowHeight = UITableView.automaticDimension
     return UITableView.automaticDimension
}

【问题讨论】:

  • 请添加您的试用代码。
  • @SGDev 你对我如何实现这一点有什么建议吗?已添加

标签: ios swift uitableview uitextview


【解决方案1】:

所以,我并没有完全达到我想要的,但已经足够接近了。我会做更多的自定义并添加到响应中,但是对于那些想要抢先一步的人来说,这里是解决方案:

  1. 向 ui 视图控制器添加了一个 uitable
  2. 将表委托和数据源分配给控制器
  3. 添加了两个动态单元格
  4. 给它们命名,例如 EntryCell 和 DisplayCell
  5. 在第二个单元格中添加了一个 textView,对边距进行了限制,并设置为不滚动但可编辑。
  6. 使视图控制器成为 textView 的委托
  7. 为视图控制器的表格和文本视图到 TableViewCell 类文件的出口制作了一个出口,您需要创建它

现在我还想要的是,当用户按下回车键时,我希望保存数据并将光标移动到下一个单元格。为此,我在 shouldChangeTextIn 中进行了更改

代码粘贴了,故事板的截图也加了,我确定说明不是太清楚,代码没有很好的注释,但请随时提出问题,我会尽力帮助你。当事物是新事物并且我不是专业的开发人员时,我知道斗争:(

import UIKit

class test2VC: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextViewDelegate {

    @IBOutlet weak var metaTable: UITableView!
    
    var metaArray = [String]()
   
    //var textView: test2TVC.txtEntry
    
    override func viewDidLoad() {
        super.viewDidLoad()

        metaTable.rowHeight = UITableView.automaticDimension
        
        metaTable.separatorInset = .zero //MakeTable Separator go to the edge
        metaTable.layoutMargins = .zero
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

        // Do any additional setup after loading the view.
        //metaArray.append("Test String")
        
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
   
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            textView.resignFirstResponder()
            
            metaArray.append(textView.text)
            //textView.text = ""
            
            metaTable.reloadData()
            scrollToBottom()
            
            //dump(metaArray)
            return false
        }
        return true
    }
    
    //hide and show keyboard and move table
    
    @objc func keyboardWillShow(_ notification:Notification) {
        
        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            metaTable.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
        }
    }
    @objc func keyboardWillHide(_ notification:Notification) {
        
        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            metaTable.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
    }
    
    func textViewDidChange(_ textView: UITextView) {
        metaTable.beginUpdates()
        metaTable.endUpdates()
    }
    
    func textViewDidBeginEditing(_ textView: UITextView)
    {
        if (textView.text == "Type something here..." && textView.textColor == .lightGray)
        {
            textView.text = ""
            textView.textColor = .black
        }
        textView.becomeFirstResponder() //Optional
    }
    
    func textViewDidEndEditing(_ textView: UITextView)
    {
        if (textView.text == "")
        {
            textView.text = "Type something here..."
            textView.textColor = .lightGray
        }
        //textView.resignFirstResponder()
    }
    
    func scrollToBottom(){
        DispatchQueue.main.async {
            let indexPath = IndexPath(row: 0, section: 1)
            self.metaTable.scrollToRow(at: indexPath, at: .bottom, animated: true)
        }
    }
    
    
    /*func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if (text as NSString).rangeOfCharacter(from: CharacterSet.newlines).location == NSNotFound {
            return true
        }
        txtView.resignFirstResponder()
        return false
    }*/
    
   
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (section == 0) {
            return metaArray.count
        } else if (section == 1){
            return 1
        }
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cellIdentifier = ""
        switch (indexPath as NSIndexPath).section {
        case 0:
            cellIdentifier = "DisplayCell"
        case 1:
            cellIdentifier = "EntryCell"
        default: ()
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! test2TVC
        
        if ((indexPath as NSIndexPath).section == 0) {
            cell.textLabel?.text = metaArray[indexPath.row]
        } else if ((indexPath as NSIndexPath).section == 1) {
            //cell.txtEntry?.text = "Test"
           
            cell.txtEntry.text = "Type something here..."
            cell.txtEntry.textColor = .lightGray
            
            
        }
      
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView //recast your view as a UITableViewHeaderFooterView
        //header.contentView.backgroundColor = UIColor(red: 204/255, green: 229/255, blue: 255/255, alpha: 1.0) //make the background color light blue
        header.contentView.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0) //make the background color white
        //header.contentView.backgroundColor = #colorLiteral(red: 0.9738872647, green: 0.9682117105, blue: 0.9014379382, alpha: 1)
        //header.textLabel!.textColor = UIColor.lightGray //make the text lightGray
        header.textLabel!.textColor = UIColor.black //make the text Black
        header.textLabel!.textAlignment = .left //Align text Center
        header.textLabel!.font = UIFont.boldSystemFont(ofSize: 25) //Header text font size
        header.alpha = 0.95 //make the header transparent
        header.alpha = 1 //make the header non-transparent
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if (section == 0) {
            return 62
        } else {
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if (section == 0){
            return " Ingredients"
        } else {
            return ""
        }
    }

    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    相关资源
    最近更新 更多