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 ""
}
}