【发布时间】:2015-12-28 00:10:11
【问题描述】:
谁能告诉我我做错了什么? 可能很多因为我是新手,但期待解决这个问题!
我正在使用 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] = []
【问题讨论】:
标签: ios iphone xcode swift parse-platform