2016-01-13 151 views
1

我想在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 
} 

回答

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中實例化這些助手類,並將它們分配給dataSourcedelegate表視圖的屬性。您的遺囑還需要在某處持有強烈的參考,因爲dataSourcedelegate屬性爲星期。

相關問題