Ios UITableview中的文本输入类似于;任务/提醒”;应用程序
目标:创建类似于iOS任务/提醒/日程安排应用程序的文本输入方法 我知道如何将Ios UITableview中的文本输入类似于;任务/提醒”;应用程序,ios,swift,uitableview,uitextview,Ios,Swift,Uitableview,Uitextview,目标:创建类似于iOS任务/提醒/日程安排应用程序的文本输入方法 我知道如何将UITextView添加到动态UITablviewCell并使其可编辑,关闭滚动并使单元格根据输入的文本动态调整其高度 我还使用textViewShouldEndEditing方法来确定光标的位置 我需要你们帮助的地方是,当用户在键盘上按enter键时,如何保持动态添加单元格,这样他们就可以开始在下面的下一个单元格中键入内容。 另外,在加载应用程序时,我当前正在将numberofrowsinssection设置为10,
UITextView
添加到动态UITablviewCell
并使其可编辑,关闭滚动并使单元格根据输入的文本动态调整其高度
我还使用textViewShouldEndEditing
方法来确定光标的位置
我需要你们帮助的地方是,当用户在键盘上按enter键时,如何保持动态添加单元格,这样他们就可以开始在下面的下一个单元格中键入内容。
另外,在加载应用程序时,我当前正在将numberofrowsinssection
设置为10,有没有办法不硬编码该值
我还想在下一个要添加的单元格中添加加号按钮,就像在iOS应用程序中一样,如果你能以正确的方式指导我,那将非常棒
提前谢谢大家
所以,我并没有完全达到我想要的,但已经足够接近了。我将进行更多的定制并添加到响应中,但对于那些希望抢先一步的人,以下是解决方案:
导入UIKit
类test2VC:UIViewController、UITableViewDataSource、UITableViewDelegate、UITextViewDelegate{
@IBVAR元表:UITableView!
var metaArray=[String]()
//var textView:test2TVC.txentry
重写func viewDidLoad(){
super.viewDidLoad()
metaTable.rowHeight=UITableView.automaticDimension
metaTable.separatorInset=.zero//MakeTable分隔符转到边缘
metaTable.layoutMargins=.0
NotificationCenter.default.addObserver(self,选择器:#选择器(keyboardWillShow),名称:UIResponder.keyboardWillShowNotification,对象:nil)
NotificationCenter.default.addObserver(self,选择器:#选择器(keyboardWillHide),名称:UIResponder.keyboardWillHideNotification,对象:nil)
//加载视图后执行任何其他设置。
//附加(“测试字符串”)
}
脱硝{
NotificationCenter.default.removeObserver(self,名称:UIResponder.keyboardWillShowNotification,对象:nil)
NotificationCenter.default.removeObserver(self,名称:UIResponder.keyboardWillHideNotification,对象:nil)
}
func textView(textView:UITextView,shouldChangeTextIn范围:NSRange,replacementText:String)->Bool{
如果文本==“\n”{
textView.resignFirstResponder()辞职
metaArray.append(textView.text)
//textView.text=“”
metaTable.reloadData()
scrollToBottom()
//转储(元数组)
返回错误
}
返回真值
}
//隐藏和显示键盘并移动表格
@objc func键盘将显示(uu通知:通知){
如果让keyboardSize=(notification.userInfo?[UIResponder.KeyboardFrameBeginUserInfo]作为?NSValue)?.cgRectValue{
metaTable.contentInset=UIEdgeInSet(顶部:0,左侧:0,底部:keyboardSize.height,右侧:0)
}
}
@objc func键盘将隐藏(uu通知:通知){
如果让keyboardSize=(notification.userInfo?[UIResponder.KeyboardFrameBeginUserInfo]作为?NSValue)?.cgRectValue{
metaTable.contentInset=UIEdgeInSet(顶部:0,左侧:0,底部:0,右侧:0)
}
}
func textViewDidChange(textView:UITextView){
metaTable.BeginUpdate()
metaTable.endUpdates()
}
func textViewDidBeginEditing(uTEXTVIEW:UITextView)
{
如果(textView.text==“在此处键入内容…”&&textView.textColor==.lightGray)
{
textView.text=“”
textView.textColor=.black
}
textView.becomeFirstResponder()//可选
}
func textViewDidEndEditing(uTEXTVIEW:UITextView)
{
如果(textView.text==“”)
{
textView.text=“在此处键入内容…”
textView.textColor=.lightGray
}
//textView.resignFirstResponder()辞职
}
func scrolltobttom(){
DispatchQueue.main.async{
设indexPath=indexPath(行:0,节:1)
self.metaTable.scrollToRow(at:indepath,at:.底部,动画:true)
}
}
/*func textView(textView:UITextView,shouldChangeTextIn范围:NSRange,replacementText:String)->Bool{
if(文本为NSString).rangeOfCharacter(from:CharacterSet.newlines).location==NSNotFound{
返回真值
}
txtView.resignFirstResponder()辞职
返回错误
}*/
func numberOfSections(在tableView:UITableView中)->Int{
返回2
}
func tableView(u tableView:UIT
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
}