ios - Table View reloadData() not working when populating Values using UserDefaults -


i working on project shows news feeds user.

i calling function getnewsfeeds() news feeds.

i generate new newsfeeds every 1 hour

    viewdidload(){      current time in "hh" format , store in currenttime      store currenttime in hoursvar variable using userdefaults      }      viewdidappear(){      if( currenttime - hoursvar >= 1){     getnewsfeeds    }else{      }     retreivestoredvaluesfromuserdeafults()     }  

i dont know setup tableviewdatasource , delegate methods , reloaddata populate tableview when getting data saved userdefaults retreivestoredvaluesfromuserdeafults()

 override func viewdidload() {     super.viewdidload()        hoursformatter.dateformat = "hh"     hoursvar = int(hoursformatter.string(from: hours))!      if hoursvar > 12 {         hoursvar = hoursvar - 12     }      self.defaults.set(self.hoursvar, forkey: "hoursvar")      customnavbar()     self.view.addgesturerecognizer(self.revealviewcontroller().pangesturerecognizer())     feedsarray = ["reliance","economy"]  }  override func viewdidappear(_ animated: bool) {      //getrssfeeds()      if((hoursvar - myhoursvar) == 1 && (hoursvar - myhoursvar) > 1){          getrssfeeds()      }else{            feedstable.register(uitableviewcell.self, forcellreuseidentifier: "newsfeeds")              self.feedstable.delegate = self             self.feedstable.datasource = self          retreivemyarraydata()          feedstableview.reloaddata()     }  }  func getrssfeeds(){      in 0..<feedsarray.count{          let url = url(string: "https://api.cognitive.microsoft.com/bing/v5.0/news/search?q=\(feedsarray[i])&count=3&mkt=en-in")         var request = urlrequest(url: url!)         request.setvalue("my subscription key", forhttpheaderfield: "ocp-apim-subscription-key")          alamofire.request(request urlrequest).responsejson{response in              if let json = response.result.value as? [string:anyobject]{                   if let value = json["value"]{                      //                     print("json \(json)")                      j in 0..<value.count{                          let items = value[j] as! [string:anyobject]                          let name = items["name"] as! string                          let url = items["url"] as! string                            let description = items["description"] as! string                          let datepublished = items["datepublished"] as! string                          let dateandtime = datepublished.replacingoccurrences(of: "t", with: " ")                          self.feedsname.append(name)                         self.feedsurl.append(url)                         self.feedsdescription.append(description)                         self.feedsdatepublished.append(dateandtime)                           if let image = items["image"] as? [string:anyobject]{                              if let thumbnail = image["thumbnail"] as? [string:anyobject]{                                  let contenturl = thumbnail["contenturl"] as! string                                  self.feedscontenturl.append(contenturl)                                }                         }else{                             self.feedscontenturl.append(self.errorimage)                          }                           if let provider = items["provider"]{                              in 0..<provider.count{                                  let items = provider[i] as! [string:anyobject]                                 let providername = items["name"] as! string                                  self.feedsprovider.append(providername)                              }                           }                          self.feedstable.delegate = self                         self.feedstable.datasource = self                         self.feedstable.reloaddata()                           self.defaults.set(self.feedsurl, forkey: "feedsurl")                         self.defaults.set(self.feedsdescription, forkey: "feedsdescription")                         self.defaults.set(self.feedsname, forkey: "feedsname")                         self.defaults.set(self.feedsprovider, forkey: "feedsprovider")                         self.defaults.set(self.feedscontenturl, forkey: "feedscontenturl")                         self.defaults.set(self.feedsdatepublished, forkey: "feedsdatepublished")                                print("all counts  \(self.feedsname.count)")                       }                 }             }          }         }       } func slideopen(){     self.revealviewcontroller().revealtoggle(animated: true) }  func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {       return feedsname.count }  func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecell(withidentifier: "newsfeeds") as! newsfeeds        randomnumberarray = randomnumber()      //self.feedsrandomarray.append(self.feedsrandom)      print("feedsrandom \(feedsrandom)")      selectedrow = randomnumberarray[indexpath.row]         let url = url(string: feedscontenturl[randomnumberarray[indexpath.row]])      do{         let data = try data(contentsof: url!)          cell.feedsimage.image = uiimage(data: data )      }catch{         print(error)     }      let tap = uitapgesturerecognizer(target: self, action: #selector(homeviewcontroller.tapfunction))      let titleattributes = [nsfontattributename: uifont.preferredfont(fortextstyle: uifonttextstyle.headline), nsforegroundcolorattributename: uicolor.purple]      let titlestring = nsattributedstring(string: feedsname[randomnumberarray[indexpath.row]] , attributes: titleattributes)     cell.feedsheadlines.isuserinteractionenabled = true     cell.feedsheadlines.addgesturerecognizer(tap)     cell.feedsheadlines.attributedtext = titlestring     cell.feedsdescription.text = feedsdescription[randomnumberarray[indexpath.row]]     cell.feedspublishername.text = feedsprovider[randomnumberarray[indexpath.row]]     cell.publishedon.text = feedsdatepublished[randomnumberarray[indexpath.row]]      //print("all counts  \(myfeedsname.count) \(myfeedsprovider.count) \(myfeedscontenturl.count) \(myfeedsurl.count) \(myfeedsdescription.count) \(myfeedsdescription.count)")      return cell }  func tapfunction(sender:uitapgesturerecognizer) {      let safarivc = sfsafariviewcontroller(url: nsurl(string: feedsurl[selectedrow]) as! url)     self.present(safarivc, animated: true, completion: nil)     safarivc.delegate = self   }  func safariviewcontrollerdidfinish(_ controller: sfsafariviewcontroller) {      controller.dismiss(animated: true, completion: nil) }  func randomnumber() -> [int] {       let feedsindex = feedsname.count - 1     var randomnumber = int(arc4random_uniform(uint32(feedsindex)))      print("randome number \(randomnumber) \(randomnumberarray)")      k in 0..<randomnumberarray.count{     while randomnumber == randomnumberarray[k] {         randomnumber = int(arc4random_uniform(uint32(feedsindex)))     }     }     randomnumberarray.append(randomnumber)     self.defaults.set(self.randomnumberarray, forkey: "randomnumberarray")       return randomnumberarray }  func retreivemyarraydata(){       myfeedsurl = defaults.stringarray(forkey: "feedsurl") ?? [string]()     myfeedsdescription = defaults.stringarray(forkey: "feedsdescription") ?? [string]()     myfeedsname = defaults.stringarray(forkey: "feedsname") ?? [string]()     myfeedsprovider = defaults.stringarray(forkey: "feedsprovider") ?? [string]()     myfeedscontenturl = defaults.stringarray(forkey: "feedscontenturl") ?? [string]()     myfeedsdatepublished = defaults.stringarray(forkey: "feedsdatepublished") ?? [string]()     myhoursvar = defaults.integer(forkey: "hoursvar")     myfeedsrandom = defaults.array(forkey: "randomnumberarray") as! [int]     print("values \(myhoursvar) \(myfeedsprovider) \(myfeedsurl.count)")        } 

you can set uitableviewdatasourceand uitableviewdelegate .xib

look this.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -