如果旧数据与新数据不匹配,我想要翻转 UIView,它位于 tableview 内。UIView 正在翻转,但问题是当获取更多表行然后 UIView 连续翻转时。
结构
struct SMatchOddsData{
var mktId:String
var runnerName:String
var back: Double
var lay: Double
init(mktId: String, runnerName:String, back: Double, lay: Double) {
self.mktId = mktId
self.runnerName = runnerName
self.back = back
self.lay = lay
}
}
class smtchOddsData {
var mtchoddsdatas:[SMatchOddsData] = []
public static let sharedInstance = smtchOddsData()
private init() {
self.mtchoddsdatas = []
}
public func add(mktId: String, runnerName:String, back: Double, lay: Double) throws {
mtchoddsdatas.append(SoccerMatchOddsData(mktId: mktId, runnerName:runnerName, back: back, lay: lay))
}
public func ReplaceAtIndex(Index:Int, mktId: String, runnerName:String, back: Double, lay: Double) throws {
mtchoddsdatas[Index] = SoccerMatchOddsData(mktId: mktId, runnerName:runnerName, back: back, lay: lay)
}
}
数组和定时器
var timer = Timer()
var mainArray = smtchOddsData.sharedInstance
在viewDidLoad中
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(LiveViewController.refreshData), userInfo: nil, repeats: true)
市场列表功能
在这个 api 中获取市场 ID
func MarketList()
{
let params = [String : AnyObject]()
APIClient<MarketList>().API_GET(Url: strUrl, Params: params, Authentication: false, Progress: false, Alert: true, Offline: false, SuperVC: self, completionSuccess: { (resultdata) in
for response in resultdata
{
self.OddsInplay(marketId: response.marketID)
}
})
{ (error) in
print(error)
}
}
OddInplay 功能
在这个函数中,我获取了我想在 tableview 中显示的所有数据。
func OddsInplay(marketId:String)
{
let params = [String : AnyObject]()
APIClient<OddInplay>().API_GET(Url: strUrl, Params: params, Authentication: false, Progress: false, Alert: true, Offline: false, SuperVC: self, completionSuccess: { (resultdata) in
self.mainArray.mtchoddsdatas.removeAll()
for i in resultdata.first?.runners ?? []
{
try? self.mainArray.add(mktId: marketId, runnerName: i.runnerName ?? "", back: i.exchangePrices?.availableToBack?.first?.price ?? 0.0, lay: i.exchangePrices?.availableToLay?.first?.price ?? 0.0)
}
self.isStarted1 = self.isStarted1 + 1
self.ReplaceOddsInplay(marketId:marketId)
self.marketTableView.reloadData()
})
{ (error) in
print(error)
}
}
ReplaceOddInplay 函数
在这个函数中,我用新数据替换旧数据
func ReplaceOddsInplay(marketId:String)
{
let params = [String : AnyObject]()
APIClient<OddInplay>().API_GET(Url: strUrl, Params: params, Authentication: false, Progress: false, Alert: true, Offline: false, SuperVC: self, completionSuccess: { (resultdata) in
for i in resultdata.first?.runners ?? []
{
var isMatched = false
var index = 0
for j in self.mainArray.mtchoddsdatas
{
if j.runnerName == i.runnerName
{
isMatched = true
break;
}
index = index + 1;
}
if isMatched == true {
try? self.mainArray.ReplaceAtIndex(Index: index, mktId: marketId, runnerName: i.runnerName ?? "", back: i.exchangePrices?.availableToBack?[0].price ?? 0.0, lay: i.exchangePrices?.availableToLay?[0].price ?? 0.0)
}
}
})
{ (error) in
print(error)
}
}
TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainArray.mtchoddsdatas.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "matchOddsCell", for: indexPath) as! SoccerMatchOddTableViewCell
let obj = mainArray.mtchoddsdatas[indexPath.row]
cell.runnerName.text = obj.runnerName
let newback = String(obj.back)
if newback == "0.0"
{
cell.backLabel.text = "-"
}
else
{
if isStarted1 > 1 && (cell.backLabel.text ?? "" != newback)
{
UIView.transition(with: cell.backView, duration: 1.0, options: [.transitionFlipFromLeft], animations: nil, completion: nil)
}
cell.backLabel.text = newback
}
let newlay = String(obj.lay)
if newlay == "0.0"
{
cell.layLabel.text = "-"
}
else
{
if isStarted1 > 1 && (cell.layLabel.text ?? "" != newlay)
{
UIView.transition(with: cell.layView, duration: 1.0, options: [.transitionFlipFromLeft], animations: nil, completion: nil)
}
cell.layLabel.text = "\(newlay)"
}
return cell
}
我只想在旧数据不等于新数据时翻转 UIView。
好吧,所以我的理论是:
cell.layLabel.text ?? ""
将始终在您调用 ""
时返回 reloadData()
。因此它总是与 newBack/newLay 不同。
您需要做的是跟踪您的 oldLay/oldBack 并检查它们而不是
cell.layLabel.text
。在类中的数组或模型中的新变量中。
试试这个:
向您的班级添加 2 个变量:
var oldLays = [String]()
& var oldBacks = [String]()
.
将您的
cellForRow
替换为:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "matchOddsCell", for: indexPath) as! SoccerMatchOddTableViewCell
let oldLay = oldLays.count > indexPath.row ? oldLays[indexPath.row]: ""
let oldBack = oldBacks.count > indexPath.row ? oldBacks[indexPath.row]: ""
let obj = mainArray.mtchoddsdatas[indexPath.row]
cell.runnerName.text = obj.runnerName
let newback = String(obj.back)
if newback == "0.0"
{
cell.backLabel.text = "-"
}
else
{
if isStarted1 > 1 && (oldBack != newback)
{
UIView.transition(with: cell.backView, duration: 1.0, options: [.transitionFlipFromLeft], animations: nil, completion: nil)
}
cell.backLabel.text = newback
}
let newlay = String(obj.lay)
if newlay == "0.0"
{
cell.layLabel.text = "-"
}
else
{
if isStarted1 > 1 && (oldLay != newlay)
{
UIView.transition(with: cell.layView, duration: 1.0, options: [.transitionFlipFromLeft], animations: nil, completion: nil)
}
cell.layLabel.text = "\(newlay)"
}
if oldLays.count <= indexPath.row {
oldLays.append("\(obj.lay)")
}else {
oldLays[indexPath.row] = "\(obj.lay)"
}
if oldBacks.count <= indexpath.row {
oldBacks.append("\(obj.back)")
}else {
oldBacks[indexPath.row] = "\(obj.back)"
}
return cell
}