如果旧数据与 swift 中的新数据不匹配,如何翻转 Tableview 中的 UIView

How to flip the UIView which is inside Tableview if older data is not matched with new data in swift

如果旧数据与新数据不匹配,我想要翻转 UIView,它位于 tableview 中 data.The UIView 正在翻转,但问题是我有 3 行或更多行,而不是连续翻转。我已经编写了用旧数据替换新数据的代码,但没有找到我做错的地方。

例如:

在下图中,蓝色 UIView 被称为 back,红色 UIView 被称为 lay.These 两个值 back 和 lay 每秒刷新一次。在行的单元格中,我比较了新旧背部,但不断翻转。

结构

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)
        }
    }

表格视图

    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.Can 我想翻转 UIView 有人帮我解决这个问题。

我的输出

好吧,我的理论是:

当您调用 reloadData() 时,

cell.layLabel.text ?? "" 将始终 return ""。因此它总是与 newBack/ newLay 不同。

您需要做的是跟踪您的 oldLay/oldBack 并检查它们而不是 cell.layLabel.text。在 class 中的数组或模型中的新变量中。

试试这个:

  1. 向您的 class 添加 2 个变量:var oldLays = [String]() & var oldBacks = [String]()

  2. 将您的 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
     }