2014-12-09 6 views
7

Tak więc próbuję utworzyć aplikację z pojedynczym ekranem, od której uruchamiam kontroler UIViewController. Oprócz tego istnieje UITextfield, przycisk i UITableView.Problemy z dodawaniem UITableView wewnątrz kontrolera UIViewController w Swift

Zasadniczo zamierzona funkcjonalność aplikacji polega na wpisaniu słowa w polu UITextField, naciśnięciu przycisku i wyświetleniu go na ekranie UITableView.

Nigdy nie mam problemu z tym, gdy przycisk dołącza wpisy UITextField do kontrolera UITableViewController na innym ekranie, ale z jakiegoś powodu mam problemy z UITableView osadzonym na UIViewControllerze ... Na moim scenorysie zrobiłem połącz UITableView jako delegat i źródło danych kontrolera UIViewController, więc nie powinno to być problemem.

Wszelkie pomysły? Mój kod został zamieszczony poniżej.

import UIKit 

var groupList:[String] = [] 

class ExistingGroups: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource 
{ 

@IBOutlet weak var addGroup: UITextField! 


@IBAction func addGroupButton(sender: AnyObject) { 

    self.view.endEditing(true) 

    var error = "" 

    if addGroup.text == "" { 

     error = "Please enter a Group!" 
    } else { 

     groupList.append(addGroup.text) 

    } 

    if error != "" { 

     var alert = UIAlertController(title:"Error In Form", message: error, preferredStyle: UIAlertControllerStyle.Alert) 
     alert.addAction(UIAlertAction(title:"OK", style: .Default, handler: { action in 

      self.dismissViewControllerAnimated(true, completion:nil) 

     })) 

     self.presentViewController(alert, animated:true, completion:nil) 

    } 

    addGroup.text = "" 


} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    addGroup.placeholder = "Enter Group Name" 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

override func touchesBegan(touches:NSSet, withEvent event:UIEvent) 
{ 
    super.touchesBegan(touches, withEvent: event) 
    self.view.endEditing(true) 

} 

func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore. 
{ 
    textField.resignFirstResponder() 
    return true; 
} 

func tableView(tableView:UITableView, numberOfRowsInSection section: Int) -> Int { 

    return groupList.count 
} 

func numberOfSectionsInTableView(tableView:UITableView) -> Int { 

    return 1 
} 


func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell { 

    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("groupcell") as UITableViewCell 

    cell.textLabel?.text = groupList[indexPath.row] 


    return cell 
} 



} 
+0

Czy załadowałeś swój tableview, gdy użytkownik zakończy wprowadzanie? – gabbler

+0

Musisz ustawić UITextFieldDelegate na siebie i ponownie załadować tabelę, gdy użytkownik wprowadzi tekst – suhit

+0

Dzięki za pomoc. Problem polega na tym, że dla self.tableView.reloadData(); wiersz kodu, otrzymuję błąd "(UITableView, numberOfRowsInSection: Int) -> Int" nie ma członka o nazwie "reloadData" " – CL8989

Odpowiedz

21

Przede wszystkim stworzyć system IBOutlet dla Twojego widoku tabeli w ten sposób:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate { 

var groupList = [String]() 
@IBOutlet weak var addGroup: UITextField! 
@IBOutlet weak var tableView: UITableView! //<<-- TableView Outlet 

// Rest of your code.. 

} 

Potem to zrobić w sposób viewDidLoad:

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "groupcell") 

    self.addGroup.delegate = self 
    tableView.delegate = self 
    tableView.dataSource = self 
} 

Po tym można reload Twoja tabela Wyświetl po dodaniu elementu do groupList. Tutaj można to zrobić:

@IBAction func addGroupButton(sender: AnyObject) { 

    // Your Code 

    } else { 

     groupList.append(addGroup.text) 
     self.tableView.reloadData() 

    } 

    //Your Code 

} 

i zaktualizować tę funkcję:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("groupcell", forIndexPath: indexPath) as UITableViewCell 

    cell.textLabel.text = self.groupList[indexPath.row] 
    return cell 
} 

I można sprawdzić ostateczny kod, który stworzyłem dla was jest HERE.

+1

To było niezwykle pomocne. Całkowicie rozwiązałem mój problem, dzięki chłopaki. – CL8989

+0

Dziękuję Ci za naprawienie mojego problemu –

+0

chętnie Ci pomożemy .. :) –

0
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    addGroup.delegate = self // You can do this in storyboard also 
    addGroup.placeholder = "Enter Group Name" 
}  

@IBAction func addGroupButton(sender: AnyObject) { 

     self.view.endEditing(true) 

     var error = "" 

     if addGroup.text == "" { 

      error = "Please enter a Group!" 
     } else { 

      groupList.append(addGroup.text) 

      //reload the table after adding text to array 
      self.tableView.reloadData(); 

     } 

     if error != "" { 

      var alert = UIAlertController(title:"Error In Form", message: error, preferredStyle: UIAlertControllerStyle.Alert) 
      alert.addAction(UIAlertAction(title:"OK", style: .Default, handler: { action in 

       self.dismissViewControllerAnimated(true, completion:nil) 

      })) 

      self.presentViewController(alert, animated:true, completion:nil) 

     } 

     addGroup.text = "" 


    } 
Powiązane problemy