有人可以告诉我,我做错了什么吗? 可能很多因为我是新手,但期待解决这个问题!
我正在使用Koloda libray(https://github.com/Yalantis/Koloda/tree/networking_example)将一个对象数组提取到卡中,以便每张卡从我的数据库中获取一行。我已经看到网络例子与Alamofire并尝试我自己的代码使用相同的例子实现Parse。它从Parse获取对象,但我无法将它们显示在卡片中。很奇怪,实际上我没有错误,控制台显示“成功检索了5个对象”,但这只是loadData()函数。
我将从笔尖显示很多文字!
我基本上想要实现的目标是什么: 我有(MyView.xib,MyView.swift)提供标签。 (我想要的是从数组中检索文本到xib中的标签)
我正在检索对象,但我没有成功将它们显示到卡中:
Successfully retrieved 5 posts.
Optional("EvUICgRQ6E")
Optional("5kC0FLKQON")
Optional("1Uyxb2M1Et")
Optional("aeJpRCG7Qn")
Optional("GDmGh3IULm")
有些错误: 如果我返回“numberOfCards”---我收到此错误:
fatal error: Array index out of range
(lldb)
如果我返回:“返回UInt(self.data.count)” 我没有收到任何错误,但卡片根本没有显示。
我是一个新手,但指出了一个好的方向,我将能够完成它。
这是我的ViewController.swift代码:
//
// ViewController.swift
// TinderCardsSwift
//
// Created by Eugene Andreyev on 4/23/15.
// Copyright (c) 2015 Eugene Andreyev. All rights reserved.
//
import UIKit
import Koloda
import pop
import Parse
import ParseUI
private var numberOfCards: UInt = 5
class ViewController: UIViewController, KolodaViewDataSource, KolodaViewDelegate {
@IBOutlet weak var kolodaView: KolodaView!
@IBOutlet weak var menuLeft: UIBarButtonItem!
@IBOutlet weak var searchRight: UIBarButtonItem!
var cardsCollection: [MyView] = []
var labelText3 = ""
var labelText4 = ""
var currentObject:PFObject?
override func viewDidLoad() {
super.viewDidLoad()
kolodaView.dataSource = self
kolodaView.delegate = self
menuLeft.target = self.revealViewController()
menuLeft.action = Selector("revealToggle:")
searchRight.target = self.revealViewController()
searchRight.action = Selector("rightRevealToggle:")
self.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
loadData()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
//MARK: Geting Data From Parse and displaying 5 posts with a print result - WORKING
func loadData (){
if (!cardsCollection.isEmpty) {
self.kolodaView.reloadData()
return
}
let query = PFQuery(className:"Countries")
query.orderByAscending("nameEnglish")
query.findObjectsInBackgroundWithBlock {
(objects:[PFObject]?, error:NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) posts.")
// Do something with the found objects
if let objects = objects {
for object in objects {
let view = NSBundle.mainBundle().loadNibNamed("MyView", owner: self, options: nil)[0] as! MyView
view.label1.text = object["nameEnglish"] as? String
view.label2.text = object["capital"] as? String
self.cardsCollection += [view]
print(object.objectId)
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
self.kolodaView.reloadData()
return
}
/*func printData () {
let dataView = NSBundle.mainBundle().loadNibNamed("MyView",
owner: self, options: nil)[0] as? MyView
if let nameEnglish = object?["nameEnglish"] as? String {
let lbl2 = dataView?.label1 as UILabel!
lbl2!.text = nameEnglish
}
//return printData()
}*/
@IBAction func logOut4(sender: AnyObject) {
// Send a request to log out a user
PFUser.logOut()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let viewController:UIViewController = UIStoryboard(name: "SlideMain", bundle: nil).instantiateViewControllerWithIdentifier("Login_Platform")
self.presentViewController(viewController, animated: true, completion: nil)
})
}
override func viewWillAppear(animated: Bool) {
if (PFUser.currentUser() == nil) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let viewController:UIViewController = UIStoryboard(name: "SlideMain", bundle: nil).instantiateViewControllerWithIdentifier("Login_Platform")
self.presentViewController(viewController, animated: true, completion: nil)
})
}
}
//MARK: IBActions
@IBAction func leftButtonTapped() {
kolodaView?.swipe(SwipeResultDirection.Left)
}
@IBAction func rightButtonTapped() {
kolodaView?.swipe(SwipeResultDirection.Right)
}
@IBAction func undoButtonTapped() {
kolodaView?.revertAction()
}
//MARK: KolodaViewDataSource
func kolodaNumberOfCards(koloda: KolodaView) -> UInt {
return numberOfCards
//return UInt (self.data.count)
//return UInt(cardsCollection.count)
}
func kolodaViewForCardAtIndex(koloda: KolodaView, index: UInt) -> UIView {
//return UIImageView(image: UIImage(named: "Card_like_\(index + 1)"))
return (NSBundle.mainBundle().loadNibNamed("MyView",
owner: self, options: nil)[0] as? MyView)!
//let view = cardsCollection[Int(index)]
//return view
/* let dataView = NSBundle.mainBundle().loadNibNamed("MyView",
owner: self, options: nil)[0] as? MyView
let parseData = data[Int(index)]
dataView?.label1?.text = parseData.labelText
dataView?.label2?.text = parseData.label2Text
return dataView!*/
}
func kolodaViewForCardOverlayAtIndex(koloda: KolodaView, index: UInt) -> OverlayView? {
return NSBundle.mainBundle().loadNibNamed("OverlayView",
owner: self, options: nil)[0] as? OverlayView
}
//MARK: KolodaViewDelegate
func kolodaDidSwipedCardAtIndex(koloda: KolodaView, index: UInt, direction: SwipeResultDirection) {
//Example: loading more cards
if index >= 3 {
numberOfCards = 5
kolodaView.reloadData()
}
}
func kolodaDidRunOutOfCards(koloda: KolodaView) {
//Example: reloading
kolodaView.resetCurrentCardNumber()
loadData()
}
func kolodaDidSelectCardAtIndex(koloda: KolodaView, index: UInt) {
UIApplication.sharedApplication().openURL(NSURL(string: "http://yalantis.com/")!)
}
func kolodaShouldApplyAppearAnimation(koloda: KolodaView) -> Bool {
return true
}
func kolodaShouldMoveBackgroundCard(koloda: KolodaView) -> Bool {
return true
}
func kolodaShouldTransparentizeNextCard(koloda: KolodaView) -> Bool {
return true
}
func kolodaBackgroundCardAnimation(koloda: KolodaView) -> POPPropertyAnimation? {
return nil
}
}
这是MyView.swift(我定义了所有内容)
//
// MyView.swift
//
//
// Created by Viorel Petrisor on 12/29/15.
// Copyright © 2015 Viorel Petrisor. All rights reserved.
//
import UIKit
class MyView: UIView {
@IBOutlet var label1: UILabel!
@IBOutlet var label2: UILabel!
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}
MyView拥有自己的笔尖。 我的项目也会放一些照片:
KolodaView in my Main StoryBoard
有人知道如何使这项工作?我现在已经挣扎了10天,仍然没有让它发挥作用。
我非常感谢一些帮助,提示!
更新:我现在正在将此函数用于loadData()
func loadData (){
if (!cardsCollection.isEmpty) {
self.kolodaView.reloadData()
return
}
let query = PFQuery(className:"Countries")
query.orderByAscending("nameEnglish")
query.findObjectsInBackgroundWithBlock {
(objects:[PFObject]?, error:NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) posts.")
// Do something with the found objects
if let objects = objects {
for object in objects {
let view = NSBundle.mainBundle().loadNibNamed("MyView", owner: self, options: nil)[0] as! MyView
view.label1.text = object["nameEnglish"] as? String
view.label2.text = object["capital"] as? String
self.cardsCollection += [view]
print(object.objectId)
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
self.kolodaView.reloadData()
return
}
使用此变量
var cardsCollection: [MyView] = []
答案 0 :(得分:0)
你能否在GitHub上添加一个问题? https://github.com/Yalantis/Koloda/issues 谢谢
答案 1 :(得分:0)
我有一个类似的问题,卡无法获取数据。显然,这是我打电话给Bundle的一个问题。正确的Swift 3.0代码如下。
func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {
let bundle = Bundle(for: MyView.self) // This gets the correct bundle for your class, as sometimes you might have many bundles.
let nib = UINib(nibName: String(describing: MyView.self), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! MyView
return view
}