I want flip UIView which is inside tableview if older data is not matched with new data.The UIView is flipping but issue I have 3 or more rows than it flipping continuously. I have written the code for replacing a new data with old data but didn't getting where I'm doing wrong.
for example :
In below image blue color UIView is called as back and in red color UIView is called as lay.These two values back and lay is refreshing at every second. In cell for row at I have compared new back with old back but is getting flipped continuously.
Struct
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)
}
}
Array and Timer
var timer = Timer()
var mainArray = smtchOddsData.sharedInstance
In viewDidLoad
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(LiveViewController.refreshData), userInfo: nil, repeats: true)
MarketList Function
In this api getting market 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 Function
In this function im getting all data which I want to display in 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 Function
In this function im replacing old data with new data
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
}
I want to flip UIView if old back is not equal to newback and if oldlay is not equal to newlay.Can someone helpme with this.
My Output
CodePudding user response:
Okay, so my theory is:
cell.layLabel.text ?? ""
will always return ""
when you call reloadData()
. Hence it will always be different than newBack/ newLay.
What you need to do is keep track of your oldLay/ oldBack and check those instead of cell.layLabel.text
. Either in an array in the class or new variables in your model.
Try this:
Add 2 variables to your class:
var oldLays = [String]()
&var oldBacks = [String]()
.Replace your
cellForRow
with: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 }