2017-02-16 90 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