在一个视图控制器中的两个表视图swift

问题描述:

我想在swift中做一个优点和缺点列表,但是每当我删除一个con时,它就会删除一个pro。我认为这是指数路径的问题被链接到利弊两个视图控制器,但我不知道如何或在哪里我可以把它们分开在一个视图控制器中的两个表视图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 
} 

如果要实现这一切,在一个视图控制器,你可以试试这个:

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中实例化这些助手类,并将它们分配给dataSourcedelegate表视图的属性。您的遗嘱还需要在某处持有强烈的参考,因为dataSourcedelegate属性为星期。