Ios Swift重新加载集合视图不工作
我不熟悉斯威夫特。我遵循一些教程来开发社交应用程序,比如twitter应用程序。我开始执行以下按钮功能。它的调用是另一个API调用,但相同的响应只需稍加修改(例如,follower属性的数量增加一个)。电话也工作得很好。但是集合视图没有重新加载。我的代码是 ViewConrollerIos Swift重新加载集合视图不工作,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我不熟悉斯威夫特。我遵循一些教程来开发社交应用程序,比如twitter应用程序。我开始执行以下按钮功能。它的调用是另一个API调用,但相同的响应只需稍加修改(例如,follower属性的数量增加一个)。电话也工作得很好。但是集合视图没有重新加载。我的代码是 ViewConroller import LBTAComponents import TRON import SwiftyJSON class HomeDatasourceController: DatasourceController {
import LBTAComponents
import TRON
import SwiftyJSON
class HomeDatasourceController: DatasourceController {
let errorMessageLabel: UILabel = {
let label = UILabel()
label.text = "Apologies something went wrong. Please try again later..."
label.textAlignment = .center
label.numberOfLines = 0
label.isHidden = true
return label
}()
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
collectionViewLayout.invalidateLayout()
}
func follow(){
print("inside controller")
Service.sharedInstance.fetchfollowHomeFeed { (homeDatasource, err) in
if let err = err {
self.errorMessageLabel.isHidden = false
if let apiError = err as? APIError<Service.JSONError> {
if apiError.response?.statusCode != 200 {
self.errorMessageLabel.text = "Status code was not 200"
}
}
return
}
self.datasource = homeDatasource
self.collectionView?.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(errorMessageLabel)
errorMessageLabel.fillSuperview() //LBTA method call
collectionView?.backgroundColor = UIColor(r: 232, g: 236, b: 241)
setupNavigationBarItems()
Service.sharedInstance.fetchHomeFeed { (homeDatasource, err) in
if let err = err {
self.errorMessageLabel.isHidden = false
if let apiError = err as? APIError<Service.JSONError> {
if apiError.response?.statusCode != 200 {
self.errorMessageLabel.text = "Status code was not 200"
}
}
return
}
self.datasource = homeDatasource
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//first section of users
if indexPath.section == 0 {
guard let user = self.datasource?.item(indexPath) as? User else { return .zero }
let estimatedHeight = estimatedHeightForText(user.bioText)
return CGSize(width: view.frame.width, height: estimatedHeight + 66)
} else if indexPath.section == 1 {
//our tweets size estimation
guard let tweet = datasource?.item(indexPath) as? Tweet else { return .zero }
let estimatedHeight = estimatedHeightForText(tweet.message)
return CGSize(width: view.frame.width, height: estimatedHeight + 74)
}
return CGSize(width: view.frame.width, height: 200)
}
private func estimatedHeightForText(_ text: String) -> CGFloat {
let approximateWidthOfBioTextView = view.frame.width - 12 - 50 - 12 - 2
let size = CGSize(width: approximateWidthOfBioTextView, height: 1000)
let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 15)]
let estimatedFrame = NSString(string: text).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
return estimatedFrame.height
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if section == 1 {
return .zero
}
return CGSize(width: view.frame.width, height: 50)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
if section == 1 {
return .zero
}
return CGSize(width: view.frame.width, height: 64)
}
}
它使用.i尝试了
self.collectionView?.reloadData()
,但没有重新加载。请帮助我解决此问题。下载我的完整源代码请帮助我我看到了您的项目并发现了您所做的问题。
在类UserCell.swift中,应使用下一个代码:
func follow(){
print("inside source")
guard let link = controller as? HomeDatasourceController else { return }
link.follow()
}
解释问题
import LBTAComponents
import TRON
import SwiftyJSON
extension Collection where Iterator.Element == JSON {
func decode<T: JSONDecodable>() throws -> [T] {
return try map{try T(json: $0)}
}
}
class HomeDatasource: Datasource, JSONDecodable {
let users: [User]
required init(json: JSON) throws {
guard let usersJsonArray = json["users"].array, let tweetsJsonArray = json["tweets"].array else {
throw NSError(domain: "com.letsbuildthatapp", code: 1, userInfo: [NSLocalizedDescriptionKey: "Parsing JSON was not valid."])
}
// self.users = usersJsonArray.map{User(json: $0)}
// self.tweets = tweetsJsonArray.map{Tweet(json: $0)}
self.users = try usersJsonArray.decode()
self.tweets = try tweetsJsonArray.decode()
}
let tweets: [Tweet]
override func footerClasses() -> [DatasourceCell.Type]? {
return [UserFooter.self]
}
override func headerClasses() -> [DatasourceCell.Type]? {
return [UserHeader.self]
}
override func cellClasses() -> [DatasourceCell.Type] {
return [UserCell.self, TweetCell.self]
}
override func item(_ indexPath: IndexPath) -> Any? {
if indexPath.section == 1 {
return tweets[indexPath.item]
}
return users[indexPath.item]
}
override func numberOfSections() -> Int {
return 2
}
override func numberOfItems(_ section: Int) -> Int {
if section == 1 {
return tweets.count
}
return users.count
}
}
这是你项目中的代码
func follow(){
print("inside source")
var link = HomeDatasourceController() // in this place you create a new local instance of class 'HomeDatasourceController'
link.follow()
}
您试图在哪里重新加载集合视图?@rmaddy after
self.datasource=homeDatasource
但它没有重新加载查看我的完整代码。您需要在问题中发布相关代码。我共享完整的源代码。您可以通过此链接“”发现预览链接工作不正常,但我假设重新加载的数据代码在异步块之外,因此无法重新加载。正如rmaddy所说,您确实需要在这里发布所有相关代码。
func follow(){
print("inside source")
var link = HomeDatasourceController() // in this place you create a new local instance of class 'HomeDatasourceController'
link.follow()
}