- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个应用程序,主要是为了学习这门语言。基本上,我正在尝试制作一个应用程序,允许您用假钱打赌 CSGO
团队将获胜。所以现在我在允许用户创建赌注的应用程序部分。然后用户可以看到他可以接受另一个用户的赌注的所有赌注。我以前做过所有这些工作,但我发现了一个错误,所以我决定修复它。现在的问题是它崩溃了。
我需要该应用程序转到另一个屏幕,您可以在其中对一个团队下注。然后您单击该框并设置您要对该特定团队下注的金额。然后当您点击底部的提交按钮
时,它会将用户信息和投注信息上传到Firebase 数据库
。然后它将返回到上一个屏幕,显示所有尚未完成的投注。所以他也应该。
问题是当我使用 POPView
Controller 返回到显示赌注的前一个 View Controller 时,它给我一个 TableView 的索引超出范围错误。奇怪的是,如果我将方法 getBetsFor
移动到 viewDidLoad
而不是 View ,那么它就可以正常工作。这样做的问题是它会出现故障,并且不会显示用户刚刚下的赌注。
我知道我的代码很糟糕,我曾尝试将其中一些代码转换为 MVC
格式,但事不宜迟,这里是我的代码。
下面的代码是从 Firebase 数据库
获取赌注的代码,然后将该数据放入对象数组,然后将对象显示到 TableView
//
// BetView.swift
// EDraft
//
// Created by Devin Tripp on 3/20/17.
// Copyright © 2017 Devin Tripp. All rights reserved.
//
import UIKit
import Foundation
import Firebase
class BetView: UIViewController, UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var teamOne: UILabel!
@IBOutlet weak var teamTwo: UILabel!
let datRef = FIRDatabase.database().reference(fromURL: "https://edraft-77b47.firebaseio.com/")
var userMoney = String()
let getData = GatherData()
var testies: String?
var teamTOne = String()
var teamTTwo = String()
let screenSize: CGRect = UIScreen.main.bounds
let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 60, width: UIScreen.main.bounds.width, height: 50))
var navItem = UINavigationItem(title: "Money: ")
let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: nil, action: #selector(sayHello(sender:)))
let backItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: nil, action: #selector(goBack(sender:)))
override func viewWillAppear(_ animated: Bool) {
self.getData.betObjects.removeAll()
self.getData.getBetsFor(completion: { (result) in
if result == true {
//show the spinning wheel thing
self.tableView.reloadData()
} else {
// error
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(navBar)
self.tableView.separatorStyle = .none
navItem.rightBarButtonItem = doneItem
navItem.leftBarButtonItem = backItem
navBar.setItems([navItem], animated: false)
teamOne.text = teamTOne
teamTwo.text = teamTTwo
/*
getData.getBetsFor(completion: { (result) in
if result == true {
self.tableView.reloadData()
} else {
}
})
*/
getData.getMoneyFromUser(username: self.getData.userName, completion: { (money) in
if money == true {
let usermoney = String(self.getData.userMoney)
//update the UI
self.navItem.title = "$" + usermoney
print(self.getData.userMoney)
} else {
print("no money found")
}
})
}
func tableView(_ tableView: UITableView, shouldUpdateFocusIn context: UITableViewFocusUpdateContext) -> Bool {
return true
}
func updateBet(_ index: Int, completion: @escaping (_ something: Bool) -> Void) {
let userID = FIRAuth.auth()?.currentUser?.uid
datRef.child("User").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let username = value?["username"] as? String
self.getData.userName = username!
// ...
self.datRef.child("Bets").observe(.childAdded, with: { snapshot in
//
// this is the unique identifier of the bet. eg, -Kfx81GvUxoHpmmMwJ9P
guard let dict = snapshot.value as? [String: AnyHashable] else {
print("failed to get dictionary from Bets.\(self.getData.userName)")
return
}
let values = ["OpposingUsername": self.getData.userName,"Show": "no"]
//var val = self.getData.betObjects[index]
//val.opposingUserName = self.getData.userName
self.datRef.child("Bets").child(self.getData.tieBetToUser[index]).observeSingleEvent(of: .value, with: { (snapshot) in
let anothaValue = snapshot.value as? NSDictionary
let user = anothaValue?["Username"] as? String
if user == self.getData.userName {
// let the user know he cannot bet his own bet
completion(false)
} else {
self.datRef.child("Bets").child(self.getData.tieBetToUser[index]).updateChildValues(values)
completion(true)
}
})
})
}) { (error) in
print(error.localizedDescription)
}
}
func getOpoosingUserNames(_ username: String,_ index: Int, completion: @escaping (_ result: Bool) -> Void ) {
let userID = FIRAuth.auth()?.currentUser?.uid
datRef.child("User").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let username = value?["username"] as? String ?? ""
self.datRef.child("Bets").child(self.getData.tieBetToUser[index]).observeSingleEvent(of: .value, with: { snapshot in
let thisValue = snapshot.value as? NSDictionary
if let thisUserName = thisValue?["Username"] as? String {
self.getData.opposingUserName = thisUserName
completion(true)
} else {
completion(false)
}
})
}) { (error) in
print(error.localizedDescription)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//if makeEmpty == true {
// return 0
//} else {
print(self.getData.betObjects.count)
return self.getData.betObjects.count
//}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cellB", for: indexPath) as! CellB
print(self.getData.betObjects[indexPath.row].userName)
cell.userNameLabel?.text = getData.betObjects[indexPath.row].userName
cell.amountOfBet?.text = getData.betObjects[indexPath.row].betAmount
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let alertController = UIAlertController(title: "Accept Bet", message: "Match the bet of " + getData.amountBets[indexPath.row], preferredStyle: .alert)
let okButton = UIAlertAction(title: "No", style: .default, handler: { (action) -> Void in
print("Ok button tapped")
})
let yesButton = UIAlertAction(title: "Yes", style: .default, handler: { (action) -> Void in
// let them know to wait a second or the bet won't go through
var waitController = UIAlertController(title: "Please Wait", message: "Your bet is being processed", preferredStyle: .alert)
self.present(waitController, animated: true, completion: nil)
//take away the usersMoney
self.takeAwayMoney(self.getData.amountBets[indexPath.row],index: indexPath.row, completion: { (result) in
if result == true {
self.updateBet(indexPath.row, completion: { (result) in
if result == true {
self.getOpoosingUserNames(self.getData.userName, indexPath.row, completion: { (anothaResult) in
if anothaResult == true {
self.dismiss(animated: true, completion: {
let successController = UIAlertController(title: "Success", message: "You have made a bet with " + self.getData.opposingUserName, preferredStyle: .alert)
let okButt = UIAlertAction(title: "Ok", style: .default, handler: nil)
successController.addAction(okButt)
self.present(successController, animated: true, completion: nil)
//lastly delete the opposing UserName
let pathIndex = IndexPath(item: indexPath.row, section: 0)
self.getData.betObjects.remove(at: indexPath.row)
self.tableView.deleteRows(at: [pathIndex], with: .fade)
self.tableView.reloadData()
print("Second")
})
self.getData.getMoneyFromUser(username: self.getData.userName, completion: { (money) in
if money == true {
self.userMoney = String(self.getData.userMoney)
//update the UI
self.navItem.title = "$" + self.userMoney
print(self.userMoney)
} else {
print("no money found")
}
})
} else {
}
//wait for the first view to load in case it uploads to fast
})
} else {
self.dismiss(animated: true, completion: {
let cannotBet = UIAlertController(title: "Failed", message: "You cannot bet with yourself dummy", preferredStyle: .alert)
let okButt = UIAlertAction(title: "Ok", style: .default, handler: nil)
cannotBet.addAction(okButt)
self.present(cannotBet, animated: true, completion: nil)
})
}
})
} else {
// user doesn't have money
//display a alert that lets the user know hes broke
print("this should print once")
self.dismiss(animated: true, completion: {
let brokeController = UIAlertController(title: "Failed", message: "Reason: You don't have enough money!", preferredStyle: .alert)
let okButt = UIAlertAction(title: "Ok", style: .default, handler: nil)
brokeController.addAction(okButt)
self.present(brokeController, animated: true, completion: nil)
})
}
var getResult = ""
print("You have taken away the users money")
print("you made it this far almost there")
//let delayInSeconds = 3.0 // 1
//DispatchQueue.main.asyncAfter(deadline: .now() + delayInSeconds) { // 2
})
return
})
alertController.addAction(okButton)
alertController.addAction(yesButton)
present(alertController, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if(editingStyle == UITableViewCellEditingStyle.delete) {
self.getData.betObjects.remove(at: indexPath.row)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//let indexPath = self.tableView.indexPathForSelectedRow!
//var DestViewController : addBets = segue.destination as! addBets
//DestViewController.teamOne = self.teamOne.text!
//DestViewController.teamOne = self.teamTTwo
if segue.identifier == "gotobetview" {
if let destination = segue.destination as? addBets {
destination.teamOne = self.teamOne.text!
destination.teamTwo = self.teamTwo.text!
}
}
}
func takeAwayMoney(_ howMuch: String, index: Int, completion: @escaping (Bool)-> ()) -> Void{
if let notMuch = Int(howMuch) {
let userID = FIRAuth.auth()?.currentUser?.uid
datRef.child("User").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let money = value?["money"] as? String ?? ""
//convert money to int
if let conMoney = Int(money) {
var conMoreMoney = conMoney
if conMoreMoney < notMuch {
print(" You don't have enough money")
completion(false)
return
} else {
conMoreMoney -= notMuch
let values = ["money": String(conMoreMoney)]
//update the users money
self.datRef.child("User").child(userID!).updateChildValues(values)
completion(true)
}
}
// ...
}) { (error) in
print(error.localizedDescription)
}
}
}
func getUserName() -> String {
let userID = FIRAuth.auth()?.currentUser?.uid
var useruser: String!
datRef.child("User").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let username = value?["username"] as? String ?? ""
self.getData.userName = username
useruser = username
// ...
}) { (error) in
print(error.localizedDescription)
}
return useruser
}
func sayHello(sender: UIBarButtonItem) {
let userInput = self.teamOne.text
var userInputArray: [String] = []
userInputArray.append(self.teamOne.text!)
userInputArray.append(self.teamTwo.text!)
performSegue(withIdentifier: "gotobetview", sender: userInputArray)
//let storyboard = UIStoryboard(name: "Main", bundle: nil)
//let homeView = storyboard.instantiateViewController(withIdentifier: "addBets")
//self.present(homeView, animated: true, completion: nil)
}
func goBack(sender: UIBarButtonItem) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let backView = storyboard.instantiateViewController(withIdentifier: "tabview")
self.present(backView, animated: true, completion: nil)
}
}
由于我无法再发布代码,我将在 github 上更新项目并在此处发布链接。
https://github.com/devintrippprojects/EDraft
如果您下载该项目,您可以随时创建自己的帐户,您将从 100 美元开始,或者您可以使用电子邮件的主要测试帐户k@k.com密码是咔嚓咔嚓
我将在明天发布数据库的外观和应用程序的外观,祝您晚安。
最佳答案
我下载并运行了您的项目。
似乎这里的问题是您应该在删除所有数据源时重新加载 tableView。
改变这个:
self.getData.betObjects.removeAll()
self.getData.getBetsFor(completion: { (result) in
if result == true {
//show the spinning wheel thing
self.tableView.reloadData()
} else {
// error
}
})
为此:
self.getData.betObjects.removeAll()
self.tableView.reloadData()
self.getData.getBetsFor(completion: { (result) in
if result == true {
//show the spinning wheel thing
self.tableView.reloadData()
} else {
// error
}
})
多了一行:
self.tableView.reloadData()
关于ios - Swift:使用 firebase 时 TableView 的索引超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43337025/
我正在对 tableView 是否呈现单元格进行单元测试。 我发现 tableView.cellForRow(at:) 返回 nil,而 tableView.dataSource?tableView(
我有 Tableview 用于显示客户下的食品订单。其中包含水平 UIStackview。在 UIStackView UIStackview 中很少有一/两行标签和一个用于显示订单项的UITableV
我有 Viewcontroller,我在其中设置了 ScrollView 。在 scrollview 中,我有一个 tableview,它位于 Container 中,就在容器下方,当我将 table
我想在滚动时让我的第一个单元格始终位于 tableview 的顶部。 任何帮助都是可观的。提前致谢... 最佳答案 这是为 UITableView 创建标题 View 的方法 - (UIView *)
在我的应用程序中,我点击一行,它会在其下方展开另一行。我想生成一种随机颜色,当我单击一行时,行背景会变成该颜色,而它下面的展开行会变成相同的颜色。如何让行生成相同的随机颜色? 我创建了一个函数来生成随
我正在为这个问题苦苦挣扎,所以我需要你的帮助。基本上我已经编写了一个复杂的 TableView Controller (使用 NSFetchedResults 协议(protocol)等)并且已经在我
我正在使用 JavaFx 2.2。我遇到了一个问题,我无法在 TableView 列中放置不同的组件。例如我有两列 1) 回答 2) 答案类型 如果 AnswerType 包含“Multiple Ch
试图弄清楚如何在 javafx 2 中禁用表列的重新排序? 最佳答案 这是解决方案: tblView.getColumns().addListener(new ListChangeListener()
我一直在寻找有关将数据刷新到 tableview 的信息。我试图直接修改模型,但我遇到了一个错误。我修改了模型,但表格没有刷新,只有当我移动一列时,表格才会显示修改后的值。 为了给你展示一个例子(13
给定一个TableView,我需要检测单元格上的双击。 tableView.setOnMouseClicked(new EventHandler() { @Override publi
我成功创建了一个 TableView,其中将特定列分配给了数据模型类。该程序可以将 csv 文件解析为其中并在表中正确显示所有内容。在此阶段,滚动不是问题。 然后我想选择特定行并将它们发送到另一个表。
我尝试了所有方法来用数据填充 TableView。下一个代码在表中插入一个新行,但数据没有出现在表中。我试图为此找到解释,但没有成功。 请帮忙。我不知道怎么了。 在 controller.java 中
我尝试了所有方法来用数据填充 TableView。下一个代码在表中插入一个新行,但数据没有出现在表中。我试图为此找到解释,但没有成功。 请帮忙。我不知道怎么了。 在 controller.java 中
我有一个通过 Tableview 显示玩家的应用程序。用户可以“添加”一个新玩家并输入他的姓名、高度、体重、图片等...保存后将被添加到 Tableview 中。他们还可以点击每个玩家,然后将他们带到
对于大学,我必须编写一个小应用程序,其想法是创建一个小公式集合。这个概念是有一个类别列表,选择类别后您会收到一个公式列表。选择所需的公式后,您将看到公式计算器的 View 。 我正在努力获得第一个UI
如果我只加载一个 TableView ,我的 View 中必须有两个 TableView ,它工作正常。但是当我尝试使用下面的方法加载两个表格 View 时,它给出了以下异常。 未捕获异常“NSRan
我是 JavaFx 的新手,我正在尝试创建一个 TableView ,而 fxml 是使用场景生成器创建的。如果我运行该程序,该表不会获取值。我发现这个问题在某种程度上符合我的要求( javaFX 2
是否可以对 tableView 进行反向排序?我搜索了很多解决方案,但没有任何效果。 效果就像whatsapp聊天。 一个普通的tableView是: ---------- label 1 -----
我有一个有趣的问题。我有两个 TableView ,一个在另一个里面。我有一个结构,我想用它来将数据添加到我的 TableView 中。该结构有 2 个字符串项和另一个具有 3 个字符串项的结构的数组
我正在使用的方法 func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath
我是一名优秀的程序员,十分优秀!