由于无法正常工作,如何将数据注入或添加到表中?

时间:2019-02-13 19:01:39

标签: swift uitableview swift4 inject

我似乎无法在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")
    }

......

1 个答案:

答案 0 :(得分:0)

我认为您的p <- cotabplot(~ memory + attitude | age + education, data = pun, panel = cotab_coindep, n = 5000, type = "assoc", test = "maxchisq", interpolate = 1:2, legend = T)

出了问题

尝试首先使其简单,例如:

cellForRowAt

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