如何在我的表中注入或添加数据,因为它不起作用?

问题描述 投票:-1回答:1

我似乎无法在Swift中更新数据!我正在尝试为朋友广播电台建立一个广播播放器应用程序,所以当一首歌改变时我需要更新播放列表视图控制器。

来自我的主视图控制器的数据是结构的一个实例。我知道有生成的数据并将其传递给表,但无论出于何种原因,数组都没有更新。我确信这很简单。

我尝试使用协议和使用函数直接注入数据。使用协议和函数,我可以通过print语句看到传递的数据。

class PlaylistVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
    //Mark: Variables ~~~~~~~~~~~~###########################################

    var sourceDatas = [PlaylistData]()

    //Mark: View Containers ~~~~~############################################
    private let bg = GradientBG()
    private let closeButton = UIButton()
    let playlistTable = UITableView()

    //Mark: Overrides ~~~~~~~~~~~~###########################################
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        playlistTable.delegate = self
        playlistTable.dataSource = self

        layoutUI()
        setupTable()
        setupClose()
    }

    //Mark: objc func's ~~~~~~~~~~~###########################################
    @IBAction func handleXButton() {
        dismiss(animated: true, completion: nil)
    }

    func handleMoreInfo(_ playlist: PlaylistData) {
        let vc = SongPopUp()
        vc.buildLables(playlist)

        vc.modalPresentationStyle = .overCurrentContext
        self.present(vc, animated: true, completion: nil )
    }
    //##################################################################  
    //Mark: Pass Data ##################################################  
    //Mark: Not Working!! ##############################################  
    //##################################################################      
    func handlePlaylist(_ with: PlaylistData) {
        print(with)
        sourceDatas.append(with)
        //sourceDatas.insert(with, at: 0)
        playlistTable.reloadData()
    }

    //Mark: Table view   ################################################
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sourceDatas.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = UITableViewCell(style: .subtitle, reuseIdentifier: "myCell")

        cell.backgroundColor = .clear
        cell.selectionStyle = .none

        tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)

        if let text = cell.textLabel {
            components.layoutHeadingLable(sender: text, title: sourceDatas[indexPath.row].heading, size: 20)
        }
        if let dtext = cell.detailTextLabel {
            components.layoutSubheadingLable(sender: dtext, title: sourceDatas[indexPath.row].subheading, size: 14)
        }

        if sourceDatas[indexPath.item].hasStoreLink {
            cell.accessoryType = .detailButton
            cell.tintColor = .white
        }

        return cell
    }

    func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath)  {
        let dataToPass = self.sourceDatas[indexPath.row]
        print("Extended~~~~~~~~~~~~~~~~~~~~")
        print(dataToPass)
        handleMoreInfo(sourceDatas[indexPath.row])
    }

    //Mark: Setup Table ~~~~~~~~~~~~~~###########################################
    func setupTable() {
        playlistTable.backgroundColor = .clear
        playlistTable.separatorStyle = .singleLine
        playlistTable.rowHeight = 45
        playlistTable.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
    }

......
swift uitableview swift4 inject
1个回答
0
投票

我认为你的cellForRowAt有问题

首先尝试简单,例如:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! UITableViewCell
        cell.textLabel?.text = sourceDatas[indexPath.row]

        return cell
    }

查看是否可以找到添加的对象。然后深入了解单元格的详细设置。

© www.soinside.com 2019 - 2024. All rights reserved.