swift - 我如何将数据注入或添加到我的表中,因为它不起作用?
问题描述
我似乎无法在 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")
}
......
解决方案
我觉得你有问题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
}
查看是否可以找到添加的对象。然后深入了解您的单元格的详细设置。
推荐阅读
- r - 使用观察事件时如何在传单中的标记和簇之间切换?
- excel - Python xlsxwriter 图表:如果系列在主轴或次轴上,有什么方法可以在图例中显示?
- c - 如何使用 parson 库构造具有多个子键的 JSON
- android - React Native WebView injectJavascript 代码在更新时被删除
- javascript - 如何防止动画图标从输入框中溢出
- c++ - 将自定义标题视图添加到表格视图
- binary - 有没有一种合理的方法来手工制作可执行的二进制文件,只使用文本编辑器
- ionic-framework - 离子应用程序中的多语言动态 - 谷歌翻译
- postgresql - PostgreSQL 复制命令给出临时表错误
- sql - 结合 CREATE TABLE、ALTER TABLE、UPDATE TABLE