import UIKit
class ViewController: UIViewController {
@IBOutlet weak var mySearchBar: UISearchBar!
@IBOutlet weak var myTableView: UITableView!
var array_Data = [ToDo]()
override func viewDidLoad() {
super.viewDidLoad()
setuptableView()
fetchingAPIData()
}
func setuptableView(){
let nib = UINib(nibName: "SearchTableViewCell", bundle: nil)
myTableView.register(nib, forCellReuseIdentifier: "cell")
}
func fetchingAPIData() {
let url = URL(string: "https://api.opendota.com/api/heroStats")
let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data,response,error) in
guard let data = data, error == nil else {
print("Error occured while accessing data")
return
}
do {
self.array_Data = try JSONDecoder().decode([ToDo].self, from: data)
}
catch {
print("Error while Decoding JSON data info Swift structure\(error)")
}
DispatchQueue.main.async {
self.search_Data = self.array_Data
self.myTableView.reloadData()
}
})
task.resume()
}
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView2: UITableView!
var AllData = [dataModel]()
override func viewDidLoad() {
super.viewDidLoad()
tableViewSetup()
fetchingAPIData()
}
func tableViewSetup() {
let nib = UINib(nibName: "APIDataTableViewCell", bundle: nil)
tableView2.register(nib, forCellReuseIdentifier: "cell")
}
func fetchingAPIData() {
let url = URL(string: "https://jsonplaceholder.typicode.com/posts")
let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data,response,error) in
guard let data = data, error == nil else {
print("Error occured while accessing data")
return
}
do {
self.AllData = try JSONDecoder().decode([dataModel].self, from: data)
}
catch {
print("Error while Decoding JSON data info Swift structure\(error)")
}
DispatchQueue.main.async {
self.tableView2.reloadData()
}
})
task.resume()
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
AllData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView2.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! APIDataTableViewCell
cell.configure(alldata: AllData[indexPath.row])
return cell
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let st = UIStoryboard(name: "Main", bundle: nil)
let vc = st.instantiateViewController(withIdentifier: "DataPassViewController") as! DataPassViewController
vc.idd = AllData[indexPath.row].id!
vc.useridd = AllData[indexPath.row].userID!
vc.titleText = AllData[indexPath.row].title!
vc.bodyText = AllData[indexPath.row].body!
self.navigationController?.pushViewController(vc, animated: true)
}
}