我有一个名为“MEVHorizontalContacts”的github存储库的集合视图。现在我正在尝试将数据从解析填充到每个单独的联系单元格中。问题是没有数据填充到每个联系单元中。当我将我的单元格数设置为大于0时,应用程序崩溃了。这是代码。我需要帮助将数据加载到每个集合视图项中。
import UIKit
import Parse
class MevHorizontalContactsExample2: MEVHorizontalContactsExample1, MEVHorizontalContactsDataSource, MEVHorizontalContactsDelegate {
var FullNameArray = [String]()
var ProfileImageArray = [PFFile]()
var horizontalContacts: MEVHorizontalContacts!
override init(frame: CGRect) {
super.init(frame: frame)
self.setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupView() {
self.translatesAutoresizingMaskIntoConstraints = false
self.horizontalContacts = MEVHorizontalContacts()
self.horizontalContacts.backgroundColor = UIColor.white
self.horizontalContacts.dataSource = self
self.horizontalContacts.delegate = self
self.addSubview(self.horizontalContacts)
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[horizontalContacts]|", options: .alignAllCenterX, metrics: nil, views: ["horizontalContacts": self.horizontalContacts]))
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[horizontalContacts]|", options: .alignAllCenterY, metrics: nil, views: ["horizontalContacts": self.horizontalContacts]))
loadMore()
}
// MARK: - MEVHorizontalContactsDataSource Methods
func numberOfContacts() -> Int {
return FullNameArray.count
}
func numberOfItems(atContactIndex index: Int) -> Int {
switch index {
case 0:
return 2
case 1:
return 3
case 2:
return 2
case 3:
return 4
default:
return 3
}
}
// pagination
func loadMore() {
// STEP 2. Find posts made by people appended to followArray
let query = PFQuery(className: " CommercialUsers")
query.addDescendingOrder("createdAt")
query.findObjectsInBackground(block: { (objects, error) -> Void in
if error == nil {
// clean up
self.FullNameArray.removeAll(keepingCapacity: false)
self.ProfileImageArray.removeAll(keepingCapacity: false)
// find related objects
for object in objects! {
self.FullNameArray.append(object.object(forKey: "FullName") as! String)
}
} else {
print(error!.localizedDescription)
}
})
}
func contact(at index: Int) -> MEVHorizontalContactsCell {
var cell: MEVHorizontalContactsCell? = self.horizontalContacts.dequeueReusableContactCell(for: index)
cell?.imageView?.image = UIImage(named: self.getImageName(at: index))
cell?.imageView?.layer.borderColor = UIColor.clear.cgColor
cell?.imageView?.layer.borderWidth = 1.0
cell?.label?.text = self.getUserName(at: index)
cell?.label?.font = UIFont.boldSystemFont(ofSize: CGFloat(12.0))
return cell!
}
func item(_ item: Int, atContactIndex index: Int) -> MEVHorizontalContactsCell {
var image: UIImage?
var labelText: String
switch item {
case 0:
labelText = "Call"
image = UIImage(named: "actionCall")
case 1:
labelText = "Email"
image = UIImage(named: "actionEmail")
case 2:
labelText = "Message"
image = UIImage(named: "actionMessage")
default:
labelText = "Call"
image = UIImage(named: "actionCall")
}
var cell: MEVHorizontalContactsCell? = self.horizontalContacts.dequeueReusableItemCell(for: index)
cell?.imageView?.image = image
cell?.imageView?.tintColor = UIColor(red: CGFloat(34 / 255.0), green: CGFloat(167 / 255.0), blue: CGFloat(240 / 255.0), alpha: CGFloat(1))
cell?.imageView?.layer.borderColor = UIColor(red: CGFloat(34 / 255.0), green: CGFloat(167 / 255.0), blue: CGFloat(240 / 255.0), alpha: CGFloat(1)).cgColor
cell?.imageView?.layer.borderWidth = 1.0
cell?.label?.text = labelText
cell?.label?.font = UIFont.boldSystemFont(ofSize: CGFloat(10.0))
return cell!
}
func horizontalContactsInsets() -> UIEdgeInsets {
return UIEdgeInsetsMake(10, 0, 10, 0)
}
func horizontalContactsSpacing() -> Int {
return 10
}
func contactSelected(at index: Int) {
print("selectedAtContactIndex - index : %zu option : %zu ")
}
func item(_ item: Int, selectedAtContactIndex index: Int) {
print("selectedAtContactIndex - index : \(index) option : \(item) ")
}
// MARK: - Generate Data Methods
func getUserName(at index: Int) -> String {
var array: [Any] = ([FullNameArray] as NSArray) as! [Any]
return array[index] as! String
}
func getImageName(at index: Int) -> String {
var array: [Any] = ["image1", "image2", "image3", "image4", "image5", "image6", "image7", "image8", "image9", "image10", "image1", "image2", "image3", "image4", "image5", "image6", "image7", "image8", "image9", "image10", "image1", "image2", "image3", "image4", "image5", "image6", "image7", "image8", "image9", "image10"]
return array[index] as! String
}
}
答案 0 :(得分:0)
对于要加载数据的表,您需要返回正确数量的单元格(大于0)。如果您在页面加载后检索数据,则最初不会加载数据(这就是崩溃的原因)。解决方案很简单。数组应该包含所有数据。它应该是可变的并初始化为空。返回单元格数量的数组计数。加载数据并附加数组。然后重新加载表。该数组应具有新的正确计数,并且数据应该加载。
答案 1 :(得分:0)
问题在于:
- 你创建一个空数组
var FullNameArray = [String]()
- 然后collectionView请求信息,但数组为空并且什么也没显示。
- 您使用函数“loadMore”向Parse请求信息,但是您从不重新加载collectionView!
- 由于对Parse的调用是异步的,因此在收到响应后需要重新加载collectionView。
解决方案:
// pagination
func loadMore() {
// STEP 2. Find posts made by people appended to followArray
let query = PFQuery(className: " CommercialUsers")
query.addDescendingOrder("createdAt")
query.findObjectsInBackground(block: { (objects, error) -> Void in
if error == nil {
// clean up
self.FullNameArray.removeAll(keepingCapacity: false)
self.ProfileImageArray.removeAll(keepingCapacity: false)
// find related objects
for object in objects! {
self.FullNameArray.append(object.object(forKey: "FullName") as! String)
}
//ADD THIS
self.horizontalContacts.reloadData()
} else {
print(error!.localizedDescription)
}
})
}
希望它有效。