ios - 一个 viewcontroller swift 中的两个 TableView

标签 ios swift uitableview swift2 tableview

我正在尝试快速制作一个优缺点列表,但每当我删除一个缺点时,它就会删除一个优点。我认为索引路径链接到正反 View Controller 是一个问题,但我不知道如何或在哪里可以将它们分开

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
}

最佳答案

如果你想在一个 View Controller 中实现所有这些,你可以试试这个:

  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 中实例化这些辅助类,并将它们分配给 TableView 的 dataSourcedelegate 属性。您还需要在某处为它们保留强引用,因为 dataSourcedelegate 属性是 week。

关于ios - 一个 viewcontroller swift 中的两个 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34774438/

相关文章:

ios - 当字符串为空时,componentsSeparatedByString 返回 1 个对象

ios - 当 "View controller-based status bar appearance"=YES 时隐藏 ios 状态栏

从后台打开应用程序时,iOS swift 3 重新加载 tableview

ios - TableViewCell 中的更改立即生效

ios - Microsoft Surface与Iphone配对

ios - Xcode 错误 - 可执行文件使用无效权利签名

ios - "cellForRowAtIndexPath"未调用 alertviewcontroller 内的 TableView

html - 解码 HTML 字符串

arrays - 将旧的 C for 循环更改为新的 Swift 循环

ios - UITableView 缺少一些分隔线