Reputation: 529
I am new to iOS, and started with some tutorials. But when I try to display the content in tableview
, it's not showing. I did all like they did in that tutorial. But still I was not able to display my contetnt in my table view.
Here my tutoial link : My link
Here my code :
import UIKit
import Alamofire
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var nameArray = [AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Alamofire.request("http://thecodeeasy.com/test/swiftjson.json").responseJSON { response in
let result = response.result
if let dict = result.value as? Dictionary<String,AnyObject>{
if let mainDict = dict["actors"] {
self.nameArray = mainDict as! [AnyObject]
self.tableView.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as? CustomTableViewCell
let title = nameArray[indexPath.row]["title"]
cell?.ContentName.text = title as? String
return cell!
}
}
Upvotes: 1
Views: 127
Reputation: 475
There are three possibilities that you are not getting data. First, your app datasource is not set properly. Please check image for that
Second, Paste your API code into viewDidAppear and check if it's working or not
============= Important ============
Third, is check your info.plist contain following code. Because whenever you are making the internet call you should have this code into your info.plist file.
Right click on info.plist and open as source code
paste the following code
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Try to run the app. Let me know if its working for you.
Please check the link for code click here
Thank you
Abhishek Sharma
Upvotes: 1
Reputation: 10182
As I can see you are using a tableview
inside a view controller. You should set delegate and data source of this tableview
to self
.
Write these 2 lines in your view controller:
self.tableView.delegate = self
self.tableView.datasource = self
You can do that with XIB too.
If you have already done that or that Doesn't work, then try printing your self.nameArray
and check if have expected data.
Upvotes: 0
Reputation: 748
Try this in top of the viewDidLoad
self.tableView.delegate = self
self.tableView.dataSource = self
Upvotes: 0