微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在另一个视图控制器中重新加载 UITableView 的数据

如何解决在另一个视图控制器中重新加载 UITableView 的数据

我正在 Swift/Storyboards 中开发一个新项目/游戏,但我遇到了这个小障碍......游戏由 2 个导航视图控制器组成,我想在第一个视图控制器上添加一个菜单式视图该人可以在其中添加想要和他一起玩的玩家(更准确地说是他们的名字),然后这些名字会显示在我添加的屏幕截图中的 tableview 中。

当您第一次输入名称并按“+”时,我设法让它工作,playerArray 附加了输入但 tableview 根本没有更新,第二次按“+”后我得到错误

" 线程 1: "尝试将第 2 行插入第 0 节,但更新后第 0 节中只有 0 行" "

我对 tableviews 完全陌生,所以即使在互联网上搜索后我也不知道如何解决这个问题

我的视图控制器代码(不要介意打印语句只是为了检查代码在哪里完成它必须做的):

class Home_ViewController: UIViewController {
    // Parameters and start values
    
    var game_mode:Int = 1
    let storage = UserDefaults.standard
    var tableviewcontroller = player_table_Controller()
    
    @IBOutlet public var NewPlayerOutlet: UITextField!
    
    // Load view
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Navigation stuff ---->
        navigationController?.navigationBar.setBackgroundImage(UIImage(),for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        navigationController?.navigationBar.isTranslucent = true
        navigationController?.navigationBar.backIndicatorImage = UIImage(systemName: "arrowshape.turn.up.backward.fill")
        // Leave this be <----
    }
    
    //dismiss keyboard after editing textfield
    override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
        view.endEditing(true)
        super.touchesBegan(touches,with: event)

    }
    
    override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
        let backItem = UIBarButtonItem()
        backItem.title = "   "
        navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
    }
    
    // View appears back
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    @IBAction func add_player(_ sender: Any){
        print("Function add player called")
        
        if NewPlayerOutlet.text == nil || NewPlayerOutlet.text == "" {

                print("No name entered")

            } else {
                let newPlayer = NewPlayerOutlet.text!
                tableviewcontroller.PlayerArray.append(newPlayer)
                print("New Player: ",newPlayer)
                print(tableviewcontroller.PlayerArray)
                
                let indexPath = IndexPath(row: tableviewcontroller.numberOfRows(inSection: 0),section: 0)
                tableviewcontroller.insertRows(at: [indexPath],with: .automatic)
                
                tableviewcontroller.reloadData()

            }
        
    }
}

以及表格视图和单元格的代码

class player_table_cell: UITableViewCell{
    
    override func setSelected(_ selected: Bool,animated: Bool) {
        super.setSelected(selected,animated: animated)
    }
    
    
    @IBOutlet weak var NameLable: UILabel!
    
    @IBAction func removePlayer (_ sender: Any){
        
    }
    
}

class player_table_Controller: UITableView{
    
    public var PlayerArray:Array = [String]()

    override func numberOfRows(inSection section: Int) -> Int {
        return PlayerArray.count
    }
    override func cellForRow(at indexPath: IndexPath) -> UITableViewCell? {
        let cell = self.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) as! player_table_cell
        let Player = PlayerArray[indexPath.row]
        
        cell.NameLable.text = Player
        print("This is called and kNown players are:",PlayerArray,"and current used is",Player)
        return cell
    }
    
    
}

screenshot of the primary viewcontroller

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。