Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Swift中创建一个表视图,其中每个单元格都推送到新的表视图_Swift - Fatal编程技术网

如何在Swift中创建一个表视图,其中每个单元格都推送到新的表视图

如何在Swift中创建一个表视图,其中每个单元格都推送到新的表视图,swift,Swift,我是Swift和应用程序开发新手。我需要创建一个Tableview,其中每个单元格都创建新的唯一Tableview。任何人谁有一个如何做到这一点的想法请帮助这是我的代码 class TableViewController1: UIViewController, UITableViewDataSource, UITableViewDelegate{ @IBOutlet weak var tblView: UITableView! var datasourceArray = [Per

我是Swift和应用程序开发新手。我需要创建一个Tableview,其中每个单元格都创建新的唯一Tableview。任何人谁有一个如何做到这一点的想法请帮助这是我的代码

class TableViewController1: UIViewController, UITableViewDataSource, UITableViewDelegate{
    @IBOutlet weak var tblView: UITableView!
    var datasourceArray = [Person]()
    let appDelegete = UIApplication.shared.delegate as! AppDelegate
    
    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
        }
        
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            fetchAndUpdateTable()
        }
        
        func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
            if editingStyle == .delete {
                
                let person = datasourceArray[indexPath.row]
                appDelegete.deleteRecord(person: person)
                fetchAndUpdateTable()
                
            }
        }
        
        func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            if indexPath.row % 2 == 0{
                let altCellColor: UIColor? = UIColor(white: 0.7, alpha: 0.5)
                cell.backgroundColor = altCellColor
            }
        }
    
    
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return datasourceArray.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
            let person = datasourceArray[indexPath.row]
            cell?.textLabel?.text = person.steppiename! + "  --  " + person.steppiedescription!
            return cell!
        }
        
    func fetchAndUpdateTable(){
        datasourceArray = appDelegete.fetchRecords()
        tblView.reloadData()
    }
    

@IBAction func addRecord(_ sender: UIButton) {
        
        var steppienameTextField: UITextField?
        var steppiedescriptionTextField: UITextField?
        
        
        //Declare Alert Message
        let dialogMessage = UIAlertController(title: "New Steppie", message: "Please Provide Steppie Name And Decsription", preferredStyle: .alert)
        
        
        //Create Ok Button With Action Handler
        let ok = UIAlertAction(title : "OK", style: .default, handler:
        {
            (action) -> Void in
            
            let steppiename = steppienameTextField?.text
            let steppiedescription = steppiedescriptionTextField?.text
            
            if steppiename != nil && steppiedescription != nil{
                self.appDelegete.insertRecord(steppiename: steppiename!, steppiedescription: steppiedescription!)
                self.fetchAndUpdateTable()
            }
            
        })
        
        //Create Cancel Button With Action handler
        
        let cancel = UIAlertAction(title: "Cancel", style: .cancel)
        { (action) -> Void in
            
            
            print("Cancel Button Tapped")
            
        }
        
        //Add OK and Cancel Button To Dialog Message
        dialogMessage.addAction(ok)
        dialogMessage.addAction(cancel)
        
        //Add Input TextField To Dialog Message
        dialogMessage.addTextField { (textField) -> Void in
            
        steppienameTextField = textField
            steppienameTextField?.placeholder = "Steppie Name"
            
            
        }
        
        dialogMessage.addTextField { (textField) -> Void in
            
            steppiedescriptionTextField = textField
            steppiedescriptionTextField?.placeholder = "Steppie Description"
        }
        
        //present Dialog message To User
        self.present(dialogMessage,animated: true,completion: nil)
        
        
        
    }


    
    
    @IBAction func backButton(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
    
    
}

这里有两种解决方案

  • 您可以实现
    func tableView(UITableView,didSelectRowAt:IndexPath)
    的方法,并导航到另一个具有表视图的ViewController

  • 点击单元格后,可以更改当前数据源。不要忘记在那之后重新加载表视图
    tableView.reloadData()
    。如果要显示其他内容(而不是个人),可以为此创建枚举

  • enum Data {
        case person(Person)
        case other(Other)
    }