如果舊資料與新資料不匹配,我想要翻轉位于 tableview 內的 UIView。UIView 正在翻轉,但問題是我有 3 行或更多行而不是連續翻轉。我已經撰寫了用舊資料替換新資料的代碼,但沒有找到我做錯的地方。
例如 :
在下圖中,藍色 UIView 被稱為 back,紅色 UIView 被稱為lay。這兩個值 back 和 layout 每秒都在重繪 。在行的單元格中,我將新背部與舊背部進行了比較,但不斷翻轉。
結構
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)
}
}
奇數播放功能
在這個函式中,我獲取了我想在 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)
}
}
表視圖
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
}
如果 old back 不等于 newback 并且 oldlay 不等于 newlay,我想翻轉 UIView。有人可以幫我解決這個問題。
我的輸出

uj5u.com熱心網友回復:
好的,所以我的理論是:
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 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/484642.html
