Swift:称重传感器位于 UICollectionView 的正确部分?
Swift: Load cell in correct section of UICollectionView?
我正在将两个相同类型的对象加载到两个单独的数组中。第一个数组是 [Post]
类型,第二个数组也是。我的 collectionView 中有两个部分,我想将第一个数组加载到第一部分,将第二个数组加载到第二部分。我正在从 firebase 获取数据并将对象附加到数组中。在我的 numberOfItemsInSection
中,我正在检查该部分是否等于一个,加载第一个,如果该部分等于两个,则加载第二个。这工作正常,但问题是它只在每个部分加载正确数量的单元格,但没有加载正确的信息。它只是从第一个数组加载先前获取的数据。我该如何解决这个问题?谢谢...
class ProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var name: String?
var posts = [Post]()
var repost = [Post]()
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(PostCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView?.register(ProfileHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier)
collectionView?.register(RepostedHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: boostedHeaderIdentifier)
self.posts.removeAll()
self.repost.removeAll()
handleFetchUserPosts { self.collectionView?.reloadData() }
handleFetchUserReposts { self.collectionView?.reloadData() }
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return posts.count
} else {
return reposts.count
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if section == 0 {
return CGSize(width: view.frame.width, height: 180)
} else {
return CGSize(width: view.frame.width, height: 30)
}
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if indexPath.section == 0 {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier, for: indexPath) as! ProfileHeader
header.profileController = self
return header
} else {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: boostedHeaderIdentifier, for: indexPath) as! RepostedHeader
return header
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: width, height: height)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
cell.profileController = self
let posts = self.posts[indexPath.item]
let reposts = self.reposts[indexPath.item]
if self.posts != [] {
cell.post = posts
}
if self.posts != [] {
cell.post = reposts
}
return cell
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
cell.profileController = self
if indexPath.section == 0 {
let posts = self.posts[indexPath.item]
if self.posts != [] {
cell.post = posts
}
}
if indexPath.section == 1 {
let reposts = self.reposts[indexPath.item]
if self.posts != [] {
cell.post = reposts
}
}
return cell
}
我认为如果不使用它,这将解决您的问题,实际上这是一种相同的代码,但如果您的对象有多个单元格,这将派上用场
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = PostCell()
if indexPath.section == 0 {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
cell.profileController = self
let posts = self.posts[indexPath.item]
if self.posts != [] {
cell.post = posts
}
}
if indexPath.section == 1 {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
let reposts = self.reposts[indexPath.item]
if self.posts != [] {
cell.post = reposts
}
}
cell.profileController = self
return cell
}
希望这会有所帮助
我正在将两个相同类型的对象加载到两个单独的数组中。第一个数组是 [Post]
类型,第二个数组也是。我的 collectionView 中有两个部分,我想将第一个数组加载到第一部分,将第二个数组加载到第二部分。我正在从 firebase 获取数据并将对象附加到数组中。在我的 numberOfItemsInSection
中,我正在检查该部分是否等于一个,加载第一个,如果该部分等于两个,则加载第二个。这工作正常,但问题是它只在每个部分加载正确数量的单元格,但没有加载正确的信息。它只是从第一个数组加载先前获取的数据。我该如何解决这个问题?谢谢...
class ProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var name: String?
var posts = [Post]()
var repost = [Post]()
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(PostCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView?.register(ProfileHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier)
collectionView?.register(RepostedHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: boostedHeaderIdentifier)
self.posts.removeAll()
self.repost.removeAll()
handleFetchUserPosts { self.collectionView?.reloadData() }
handleFetchUserReposts { self.collectionView?.reloadData() }
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return posts.count
} else {
return reposts.count
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if section == 0 {
return CGSize(width: view.frame.width, height: 180)
} else {
return CGSize(width: view.frame.width, height: 30)
}
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if indexPath.section == 0 {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier, for: indexPath) as! ProfileHeader
header.profileController = self
return header
} else {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: boostedHeaderIdentifier, for: indexPath) as! RepostedHeader
return header
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: width, height: height)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
cell.profileController = self
let posts = self.posts[indexPath.item]
let reposts = self.reposts[indexPath.item]
if self.posts != [] {
cell.post = posts
}
if self.posts != [] {
cell.post = reposts
}
return cell
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
cell.profileController = self
if indexPath.section == 0 {
let posts = self.posts[indexPath.item]
if self.posts != [] {
cell.post = posts
}
}
if indexPath.section == 1 {
let reposts = self.reposts[indexPath.item]
if self.posts != [] {
cell.post = reposts
}
}
return cell
}
我认为如果不使用它,这将解决您的问题,实际上这是一种相同的代码,但如果您的对象有多个单元格,这将派上用场
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = PostCell()
if indexPath.section == 0 {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
cell.profileController = self
let posts = self.posts[indexPath.item]
if self.posts != [] {
cell.post = posts
}
}
if indexPath.section == 1 {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
let reposts = self.reposts[indexPath.item]
if self.posts != [] {
cell.post = reposts
}
}
cell.profileController = self
return cell
}
希望这会有所帮助