2016-01-13 5 views
1

Я пытаюсь создать список плюсов и минусов в swift, но всякий раз, когда я удаляю con, он удаляет pro. Я думаю, что это проблема с индексом пути они связаны с обоими плюсы и минусы просмотра контроллер, но я не знаю, как и где я могу отделить ихдва вида таблицы в одном viewcontroller swift

class prosConsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 
{ 
@IBOutlet var prosTableViewOutlet: UITableView! 
@IBOutlet var consTableViewOutlet: UITableView! 



@IBOutlet var tableViewOutlet: UITableView! 

var colleges : [NetCollege] = [] 


@IBOutlet var consTableView: UITableView! 
var collegesTwo : [NetCollegeTwo] = [] 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    if tableView == tableViewOutlet 
    { 
     return colleges.count 
    } 
    else 
    { 
     return collegesTwo.count 
    } 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    if tableView == tableViewOutlet 
    { 
     let cell = tableViewOutlet.dequeueReusableCellWithIdentifier("cellID") as! tableViewCell 
     //the line under maybe? 
     let college = colleges[indexPath.row] 

     cell.textLabel?.text = college.name 


     return cell 

    } 
    else 
    { 
     let cellTwo = consTableView.dequeueReusableCellWithIdentifier("IDCell") as! tableViewCell 
     let collegeTwo = collegesTwo[indexPath.row] 

     cellTwo.textLabel?.text = collegeTwo.conName 


     return cellTwo 

    } 
} 



override func viewDidLoad() 
{ 
    super.viewDidLoad() 
    editButtonItem().tag = 0 

    func shouldAutorotate() -> Bool { 
     return false 
    } 

    func supportedInterfaceOrientations() -> Int { 
     return UIInterfaceOrientation.LandscapeRight.rawValue 
    } 


} 




@IBAction func plusButtonTwo(sender: UIBarButtonItem) 
{ 
    let alertTwo = UIAlertController(title: "Add Con", message: nil, preferredStyle: .Alert) 
      alertTwo.addTextFieldWithConfigurationHandler 
       { (textField) -> Void in 
        textField.placeholder = "Add Con Here" 
      } 
      let cancelActionTwo = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) 
      alertTwo.addAction(cancelActionTwo) 


      let addActionTwo = UIAlertAction(title: "Add", style: .Default) { (action) -> Void in 
       let addCollegesTextFieldTwo = (alertTwo.textFields?[0])! as UITextField 

       let netCollegeTwo = NetCollegeTwo(nameTwo: addCollegesTextFieldTwo.text!) 


       self.collegesTwo.append(netCollegeTwo) 
       self.consTableView.reloadData() 
      } 

      alertTwo.addAction(addActionTwo) 
      self.presentViewController(alertTwo, animated: true, completion: nil) 


} 
@IBAction func onTappedPlusButton(sender: UIBarButtonItem) 
{ 

    let alert = UIAlertController(title: "Add Pro", message: nil, preferredStyle: .Alert) 
    alert.addTextFieldWithConfigurationHandler 
     { (textField) -> Void in 
     textField.placeholder = "Add Pro Here" 
     } 
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) 
    alert.addAction(cancelAction) 


    let addAction = UIAlertAction(title: "Add", style: .Default) { (action) -> Void in 
     let addCollegesTextField = (alert.textFields?[0])! as UITextField 

     let netCollege = NetCollege(name: addCollegesTextField.text!) 


     self.colleges.append(netCollege) 
     self.tableViewOutlet.reloadData() 
    } 

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

} 



    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
    if editingStyle == UITableViewCellEditingStyle.Delete 
    { 

      colleges.removeAtIndex(indexPath.row) 
      tableViewOutlet.reloadData() 

    } 

func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{ 
    return true 
} 

ответ

4

Если вы хотите реализовать все это в одном окне контроллер, вы можете попробовать это:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
    if editingStyle == UITableViewCellEditingStyle.Delete 
    { 
     if tableView == tableViewOutlet 
     { 
      colleges.removeAtIndex(indexPath.row) 
      tableView.reloadData() 
     } 
     else 
     { 
      collegesTwo.removeAtIndex(indexPath.row) 
      tableView.reloadData() 
     } 
    } 
} 

Но в этом случае лучшим решением было бы создать два класса, называемые как DataSourceOne, DataSourceTwo (или TableViewModelOne, TableViewModelTwo), а также осуществлять все связанные с логикой там. Это даже может быть два экземпляра только одного класса DataSource, в зависимости от того, что именно вам нужно. Затем вы можете создавать экземпляры этих вспомогательных классов в viewDidLoad и назначать их dataSource и delegate свойствам ваших табличных видов. Вам также нужно будет держать ссылку на них где-то, потому что dataSource и delegate недвижимости - неделя.

Смежные вопросы