2017-02-16 61 views
0

我有一个集合视图,它关闭名为“MEVHorizo​​ntalContacts”的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

哪里是所有的'collectionView'委托和数据源方法?你正在返回'cellForItemAt'之外的单元格,等等。这个伪代码还是真实的? – garrettmurray

+0

这是一个自定义集合视图。它在这里被称为 – john

+0

func getUserName(在索引:Int) - > String {array}:[Any] = FullNameArray 返回数组[索引] as!字符串 } – john

回答

0

问题是这样的:

- 你创建一个空的数组

var FullNameArray = [String]() 

那么此的CollectionView要求提供信息,但数组是空的,说明不了什么。

- 您使用函数“loadMore”请求解析信息,但您永远不会重新加载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) 
        } 
       }) 

} 

希望工程。

+0

它没有奏效。收集视图为空 – john

+0

你想看到图片 – john

+0

你认为怎么样 – john

0

对于表格加载需要返回细胞的正确数目(这将是大于0)中的数据。如果您在页面加载后检索数据,则最初没有要加载的数据(这是它崩溃的原因)。解决方案很简单。一个数组应该包含所有的数据。它应该是可变的并且初始化为空。返回单元数量的数组数量。加载数据并追加数组。然后重新加载表格。数组应该有新的正确计数并且数据应该加载。

+0

但那不是发生。我正在检索数组 – john

+0

中的零信息,请再看一遍。我做了你所说的一切,我再也没有收到任何信息 – john

相关问题