使用多个集合视图时不会加载单元格
Cells won't load when using multiple collection views
我有两个集合视图,它们从从 url 会话函数追加的两个数组中获取数据。我删除了所有内容并重新开始,但我遇到了同样的问题。我添加了一个断点,数组被填满了。我也在其他解决方案上看到, reloadData()
在数组填充后使用但对我不起作用
主视图控制器:
//Variable declarations
var recentlyPlayed = [RecentlyPlayed]()
private var info = UserDefaults.standard.dictionary(forKey: "parseJSON")
var userLikedSongs = [LikedSongs]()
var refreshControl: UIRefreshControl!
@IBOutlet weak var LikedSongsCollectionView: UICollectionView!
@IBOutlet weak var RecentlyPlayedCollectionView: UICollectionView!
@IBOutlet weak var scrollView: UIScrollView!
//Once view has loaded
override func viewDidLoad() {
super.viewDidLoad()
//Assign Collection views to self
RecentlyPlayedCollectionView.delegate = self
RecentlyPlayedCollectionView.dataSource = self
LikedSongsCollectionView.delegate = self
LikedSongsCollectionView.dataSource = self
// Get user id and users recently played songs
let user = getId()
let id = Int(user)!
retriveRecentSongs(info: id)
retriveLikedSongs(info: id)
RecentlyPlayedCollectionView.reloadData()
LikedSongsCollectionView.reloadData()
//Hide navigation bar
self.navigationController?.isNavigationBarHidden = true
}
// Define Recent songs collection view count
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if(collectionView == RecentlyPlayedCollectionView) {
return recentlyPlayed.count
} else {
return userLikedSongs.count
}
}
//Set content inside recently collection view cells
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
if (collectionView == LikedSongsCollectionView) {
let likeCell = collectionView.dequeueReusableCell(withReuseIdentifier: "LikedCell", for: indexPath) as! LikedSongsCollectionViewCell
likeCell.LikedData = userLikedSongs[indexPath.row]
return likeCell
} else {
let recentCell = collectionView.dequeueReusableCell(withReuseIdentifier: "RecentlyPlayedCell", for: indexPath) as! RecentlyPlayedCollectionViewCell
recentCell.Recentdata = recentlyPlayed[indexPath.row]
return recentCell
}
}
//Get users recenlty played song with URL session
func retriveRecentSongs(info: Int) {
let url = URL(string: "http://127.0.0.1/musicfiles/getRecentlyPlayed.php?info=" + String(info))
URLSession.shared.dataTask(with: url!) { data, response, error in
let retrievedList = String(data: data!, encoding: String.Encoding.utf8)
print(retrievedList!)
self.parseRecentSongs(data: retrievedList!)
}
.resume()
print("Getting songs")
}
func parseRecentSongs (data: String) {
if (data.contains("*")) {
let dataArray = (data as String).split(separator: "*").map(String.init)
for item in dataArray {
let itemData = item.split(separator: ",").map(String.init)
let newSong = RecentlyPlayed(id: itemData[0], songName: itemData[1], trackName: itemData[2], artist: itemData[3], owner: itemData[4], cover: itemData[5])
recentlyPlayed.append(newSong)
}
}
}
如果需要也可以添加 RecentlyPlayedCollectionViewCell 和 LikedSongsCollectionView。
您的函数 retriveRecentSongs
包含一个异步闭包。这意味着即使在它被调用并返回后,它内部的代码仍会继续执行。
func retriveRecentSongs(info: Int) {
let url = URL(string: "http://127.0.0.1/musicfiles/getRecentlyPlayed.php?info=" + String(info))
/// See here!
URLSession.shared.dataTask(with: url!) { data, response, error in
let retrievedList = String(data: data!, encoding: String.Encoding.utf8)
print(retrievedList!)
self.parseRecentSongs(data: retrievedList!)
}
.resume()
print("Getting songs")
}
您可能会注意到 print("Getting songs")
在 之前 print(retrievedList!)
.
的打印方式
打印“获取歌曲”时,您才刚刚开始URL
任务,下载尚未完成。此时,recentlyPlayed
还是空的。
retriveRecentSongs(info: id) /// started the download
RecentlyPlayedCollectionView.reloadData() /// but at this point, has not completed yet.
下载完成后,您需要调用reloadData
。
func retriveRecentSongs(info: Int) {
let url = URL(string: "http://127.0.0.1/musicfiles/getRecentlyPlayed.php?info=" + String(info))
URLSession.shared.dataTask(with: url!) { data, response, error in
let retrievedList = String(data: data!, encoding: String.Encoding.utf8)
print(retrievedList!)
/// ok, the download finished, parse the songs
self.parseRecentSongs(data: retrievedList!)
}
.resume()
print("Getting songs")
}
func parseRecentSongs (data: String) {
if (data.contains("*")) {
let dataArray = (data as String).split(separator: "*").map(String.init)
for item in dataArray {
let itemData = item.split(separator: ",").map(String.init)
let newSong = RecentlyPlayed(id: itemData[0], songName: itemData[1], trackName: itemData[2], artist: itemData[3], owner: itemData[4], cover: itemData[5])
recentlyPlayed.append(newSong)
}
}
/// now, do reloadData.
RecentlyPlayedCollectionView.reloadData()
}
还要确保删除
RecentlyPlayedCollectionView.reloadData()
LikedSongsCollectionView.reloadData()
里面 viewDidLoad()
.
我有两个集合视图,它们从从 url 会话函数追加的两个数组中获取数据。我删除了所有内容并重新开始,但我遇到了同样的问题。我添加了一个断点,数组被填满了。我也在其他解决方案上看到, reloadData()
在数组填充后使用但对我不起作用
主视图控制器:
//Variable declarations
var recentlyPlayed = [RecentlyPlayed]()
private var info = UserDefaults.standard.dictionary(forKey: "parseJSON")
var userLikedSongs = [LikedSongs]()
var refreshControl: UIRefreshControl!
@IBOutlet weak var LikedSongsCollectionView: UICollectionView!
@IBOutlet weak var RecentlyPlayedCollectionView: UICollectionView!
@IBOutlet weak var scrollView: UIScrollView!
//Once view has loaded
override func viewDidLoad() {
super.viewDidLoad()
//Assign Collection views to self
RecentlyPlayedCollectionView.delegate = self
RecentlyPlayedCollectionView.dataSource = self
LikedSongsCollectionView.delegate = self
LikedSongsCollectionView.dataSource = self
// Get user id and users recently played songs
let user = getId()
let id = Int(user)!
retriveRecentSongs(info: id)
retriveLikedSongs(info: id)
RecentlyPlayedCollectionView.reloadData()
LikedSongsCollectionView.reloadData()
//Hide navigation bar
self.navigationController?.isNavigationBarHidden = true
}
// Define Recent songs collection view count
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if(collectionView == RecentlyPlayedCollectionView) {
return recentlyPlayed.count
} else {
return userLikedSongs.count
}
}
//Set content inside recently collection view cells
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
if (collectionView == LikedSongsCollectionView) {
let likeCell = collectionView.dequeueReusableCell(withReuseIdentifier: "LikedCell", for: indexPath) as! LikedSongsCollectionViewCell
likeCell.LikedData = userLikedSongs[indexPath.row]
return likeCell
} else {
let recentCell = collectionView.dequeueReusableCell(withReuseIdentifier: "RecentlyPlayedCell", for: indexPath) as! RecentlyPlayedCollectionViewCell
recentCell.Recentdata = recentlyPlayed[indexPath.row]
return recentCell
}
}
//Get users recenlty played song with URL session
func retriveRecentSongs(info: Int) {
let url = URL(string: "http://127.0.0.1/musicfiles/getRecentlyPlayed.php?info=" + String(info))
URLSession.shared.dataTask(with: url!) { data, response, error in
let retrievedList = String(data: data!, encoding: String.Encoding.utf8)
print(retrievedList!)
self.parseRecentSongs(data: retrievedList!)
}
.resume()
print("Getting songs")
}
func parseRecentSongs (data: String) {
if (data.contains("*")) {
let dataArray = (data as String).split(separator: "*").map(String.init)
for item in dataArray {
let itemData = item.split(separator: ",").map(String.init)
let newSong = RecentlyPlayed(id: itemData[0], songName: itemData[1], trackName: itemData[2], artist: itemData[3], owner: itemData[4], cover: itemData[5])
recentlyPlayed.append(newSong)
}
}
}
如果需要也可以添加 RecentlyPlayedCollectionViewCell 和 LikedSongsCollectionView。
您的函数 retriveRecentSongs
包含一个异步闭包。这意味着即使在它被调用并返回后,它内部的代码仍会继续执行。
func retriveRecentSongs(info: Int) {
let url = URL(string: "http://127.0.0.1/musicfiles/getRecentlyPlayed.php?info=" + String(info))
/// See here!
URLSession.shared.dataTask(with: url!) { data, response, error in
let retrievedList = String(data: data!, encoding: String.Encoding.utf8)
print(retrievedList!)
self.parseRecentSongs(data: retrievedList!)
}
.resume()
print("Getting songs")
}
您可能会注意到 print("Getting songs")
在 之前 print(retrievedList!)
.
打印“获取歌曲”时,您才刚刚开始URL
任务,下载尚未完成。此时,recentlyPlayed
还是空的。
retriveRecentSongs(info: id) /// started the download
RecentlyPlayedCollectionView.reloadData() /// but at this point, has not completed yet.
下载完成后,您需要调用reloadData
。
func retriveRecentSongs(info: Int) {
let url = URL(string: "http://127.0.0.1/musicfiles/getRecentlyPlayed.php?info=" + String(info))
URLSession.shared.dataTask(with: url!) { data, response, error in
let retrievedList = String(data: data!, encoding: String.Encoding.utf8)
print(retrievedList!)
/// ok, the download finished, parse the songs
self.parseRecentSongs(data: retrievedList!)
}
.resume()
print("Getting songs")
}
func parseRecentSongs (data: String) {
if (data.contains("*")) {
let dataArray = (data as String).split(separator: "*").map(String.init)
for item in dataArray {
let itemData = item.split(separator: ",").map(String.init)
let newSong = RecentlyPlayed(id: itemData[0], songName: itemData[1], trackName: itemData[2], artist: itemData[3], owner: itemData[4], cover: itemData[5])
recentlyPlayed.append(newSong)
}
}
/// now, do reloadData.
RecentlyPlayedCollectionView.reloadData()
}
还要确保删除
RecentlyPlayedCollectionView.reloadData()
LikedSongsCollectionView.reloadData()
里面 viewDidLoad()
.