Home.swift
在我的TableViewCell数据中从CoreData检索数据,
func getMainHomeTheme(){
let entityDescription = NSFetchRequest<NSFetchRequestResult>(entityName: "MainThemeHome")
do{
let results = try context.fetch(entityDescription)
if(results.count) > 0 {
sections.removeAll()
for i in 0 ..< (results.count){
let match = results[i] as! NSManagedObject
// let provider_ID = match.value(forKey: "providerID") as! Int
main_associated_url = match.value(forKey: "main_associated_url") as! String
main_created_by = match.value(forKey: "main_created_by") as! Bool
main_created_date = match.value(forKey: "main_created_date") as! String
main_description = match.value(forKey: "main_description_") as! String
main_name = match.value(forKey: "main_name") as! String
main_poster_image_url = match.value(forKey: "main_poster_image_url") as! String
// let provider_id = match.value(forKey: "provider_id") as! Int
main_modify_by = match.value(forKey: "main_modify_by") as! Bool
main_modify_date = match.value(forKey: "main_modify_date") as! String
main_status = match.value(forKey: "main_status") as! Bool
// let screen_id = match.value(forKey : "screen_id") as! Bool
debugPrint(main_associated_url)
}
//debugPrint("Count Deatil \(mainHomeThemeTable.count)")
}else{
//no WatchLater Data
}
}catch{}
}
来自CoreData的数据显示TableViewCell,
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
debugPrint("Home Category Cout \(mainHomeThemeTable.count)")
return mainHomeThemeTable.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let mainThemeList = mainHomeThemeTable[(indexPath as NSIndexPath).row]
self.prefs.set(mainThemeList.main_associated_url, forKey: "associated_home_url")
let cell = tableView.dequeueReusableCell(withIdentifier: "homecell") as! HomeCategoryRowCell
cell.categoryTitle.text = mainThemeList.main_name
cell.mainAssociatedURL.text = mainThemeList.main_associated_url
cell.categoryTitle.font = UIFont.boldSystemFont(ofSize: 17.0)
cell.collectionView.reloadData()
return cell
}
在显示TableViewCell数据之后,我设置了(associated_home_url)的首选项,该链接数据从服务器检索并在CollectionViewCell中显示。
HomeCategoryRowCell.swift
func getAssetsData(){
if Reachability().isInternetAvailable() == true {
associated_url = prefs.value(forKey: "associated_home_url") as! String!
rest.get(url: StringResource().mainURL + associated_url , parma: [ "show_min": "true" ], finished: {(result : NSDictionary, status : Int) -> Void in
self.assetsTable.removeAll()
if(status == 200){
let data = result["data"] as! NSArray
if (data.count>0){
for item in 0...(data.count) - 1 {
let themes : AnyObject = data[item] as AnyObject
let created = themes["created"] as! String
let assets_id = themes["id"] as! Int
let name = themes["name"] as! String
var poster_img_url = themes["poster_image_url"] as! String
let provider_id = themes["provider_id"] as! Int
self.assetsTable.append(AssetsTableItem(created: created,assets_id: assets_id, name: name, poster_image_url: poster_img_url,provider_id: provider_id))
}
}
}
})
}
}
来自Server的接收数据显示在collectionViewCell中,
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return assetsTable.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! HomeVideoCell
let list = assetsTable[(indexPath as NSIndexPath).row]
AsyncImageLoader.sharedLoader.imageForUrl(urlString: list.poster_url) { (image, url) -> () in
DispatchQueue.main.async(){
cell.movieTitle.text = list.name
cell.imageView.image = image
}
}
return cell
}
我的问题是,TableViewCell prefs数据未正确检索CollectionViewCell中的数据。 如何在TableViewCell数据显示中解决CollectionViewCell?