【问题标题】:How do I add texts into each cell using textfield and button?如何使用文本字段和按钮将文本添加到每个单元格中?
【发布时间】:2015-12-16 06:35:37
【问题描述】:

如何将文本添加到tableViewCell?我拖了一个textfieldbutton,并在它们下面添加,设置tableViewtableviewCell。单元格的标识符是“Cell”。这是我到目前为止制作的代码。理想情况下,我想将在textfield 中输入的文本添加到单元格中,例如,如果我添加我的名字,它将被添加,如果下一个人添加她的名字,她的名字也会出现在单元格上。

@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var textField: UITextField!

var stringArray = [String]()
@IBOutlet weak var AddBUtton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    textField.delegate = self
}

func textFieldDidBeginEditing(textField: UITextField) {
}
func textFieldDidEndEditing(textField: UITextField) {
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    return true
}
func textFieldShouldClear(textField: UITextField) -> Bool {
    return true
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "Cell"
    let cell = self.myTableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell 
    return cell
}

【问题讨论】:

    标签: ios swift uitableview uitextfield


    【解决方案1】:

    尝试设置UITableViewCell的textLabel

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        stringArray.append(textField.text)
        textField.text = nil
        textField.resignFirstResponder()
        table.reloadData() // if possible just reload or insert only the cell
        return true
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        let cell = self.myTableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell 
        cell.textLabel.text = stringArray[indexPath.row]
        return cell
    }
    

    【讨论】:

    • 谢谢!有效!!现在我想如果我可以在这个项目中添加更多功能会非常酷,比如当我在我的文本字段中输入单词时,它会按字母顺序排列。你知道怎么做吗?
    • 每次将字符串添加到数组时,您只需在重新加载之前对其进行排序,例如。 1) stringArray.append(textField.text) 2) stringArray.sort //怎么排序,mm自己想办法
    【解决方案2】:

    在自定义 UITableViewCell 中尝试this control。每次在文本框中输入名称时,将该名称添加到该控件的数据源中,以便下次在建议列表中出现。您可以轻松地在用户键入时过滤文本(如果需要)。

    【讨论】:

      【解决方案3】:

      您可以创建一个继承自UITableViewCell 的自定义UITableViewCell 类。从那里您可以添加一个变量来存储您从textfield 获得的任何文本。

      之后,您可以将该单元格用作cellForRowAtIndexPath 方法中的新单元格。

      【讨论】:

      • 感谢您的参与!这个问题解决了!
      【解决方案4】:

      尝试做这样的事情:

      @IBOutlet weak var text: UITextField!
      
      @IBOutlet weak var button: UIButton!
      @IBOutlet weak var tableView: UITableView!
      
      var name:NSMutableArray = NSMutableArray();
      
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
          self.tableview.delegate = self;        
          self.tableView.dataSource = self;
      }
      
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
      
      @IBAction func buttonClicked(sender: AnyObject) {
          if(self.text.text == ""){
              let alert = UIAlertController(title: "Alert", message: "Enter a name", preferredStyle: UIAlertControllerStyle.Alert)
              alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
              self.presentViewController(alert, animated: true, completion: nil);
          }
          self.name.addObject(self.text.text!)
          self.text.text = "";
          self.tableView.reloadData();
      }
      
      func numberOfSectionsInTableView(tableView: UITableView) -> Int {
          return 1;
      }
      
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
          let cellIdentifier = "Cell";
          var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier);
          if(cell == nil){
              cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier);
          }
      
          cell?.textLabel?.text = self.name[indexPath.row] as? String
      
          return cell!;
      }
      
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return self.name.count;
      }
      

      【讨论】:

      • 感谢您的时间和帮助!这个演示对我这个初学者来说有点困难,但值得知道我将尝试学习能够使用的很酷的功能;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多