In my code I have an issue that reads "Value of type 'NSManagedObject' has no member 'allObjects' ". What would I need to include in my code to make the allObjects reference work? I am watching this tutorial on coding this simple app that lists jokes using table views. Thanks in advance!
import UIKit
class jokesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet var tableView: UITableView!
var collection: Collection?
var jokes = [Joke]()
override func viewDidLoad() {
super.viewDidLoad()
self.jokes = self.collection?.jokes?.allObjects as! [Joke]
self.tableView.delegate = self
self.tableView.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.jokes.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let joke = self.jokes[indexPath.row]
cell.textLabel?.text = joke.title
return cell
}
}
Related
i'm set tableview datasource but it doesn't recognize with his erorr
Type 'ViewController' does not conform to protocol 'UITableViewDataSource'
import UIKit
class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{
override func viewDidLoad() {
super.viewDidLoad()
var contact : [pedped]=[]
let asghar = pedped()
asghar.nmae="asghar "
asghar.lname="ghasemi"
asghar.image="asghar"
let akbar = pedped()
akbar.nmae="akbar "
akbar.lname="askj"
akbar.image="asgkajsjkashar"
contact.append(asghar)
contact.append(akbar)
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contact.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath)as!custom
let contacts = contact[indexPath.row]
cell.lablename.text = contacts.nmae
cell.lastnamelable.text = contacts.lname
return cell
}
}
}
Although ViewController conforms to UITableViewDataSource, you are implementing the methods in wrong place. You have to remove them outside viewDidLoad.
Also, make sure that the table view data source/delegate is connected to the view controller itself, whether from the interface build or from the code:
// create an IBOutlet for the table view then:
tableView.dataSource = self
tableView.delegate = self
This probably won't work 100% but it at least shows you a better way to organize your code
import UIKit
class ViewController: UIViewController {
var contact = [pedped]() // Int
override func viewDidLoad() {
super.viewDidLoad()
let asghar = pedped()
asghar.nmae="asghar "
asghar.lname="ghasemi"
asghar.image="asghar"
let akbar = pedped()
akbar.nmae="akbar "
akbar.lname="askj"
akbar.image="asgkajsjkashar"
contact.append(asghar)
contact.append(akbar)
}
}
extension ViewController: UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contact.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath)as!custom
let contacts = contact[indexPath.row]
cell.lablename.text = contacts.nmae
cell.lastnamelable.text = contacts.lname
return cell
}
}
extension ViewController: UITableViewDelegate {
// Put any UITableViewDelegate functions here
}
I'm trying to get data from API and display it in the tableView.
Because getting data from API used multiple times in my application. So I want to make it reusable.
I create a class called GetProducts to get the data from API, and save it to Posts
class GetProducts{
static var products = [Product]()
static func loadProducts( _ filter: [String:String]) {
// get data from API
// append products to products list
}
}
In the viewController, I want to fetch the products data from GetProducts class, and display it in the tableView
class MainViewController: UIViewController,UITableViewDataSource, UITableViewDelegate{
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
GetPeoducts.loadProducts([...])
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return GetProducts.products.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(for: indexPath) as ProductTableViewCell
cell.products = GetProducts.products
return cell
}
}
However, in the tableView, the products list is empty.
I cannot figure it out which step was wrong. Is there anyone can help me with logic?
Thanks in advance!
Basically, what I'm doing is using completion handler
class GetPosts{
static var products = [Product]()
static func loadProducts( _ filter: [String:String], completion:(([Product]) -> Void)?) {
// get data from API
// append products to products list
completion?(self.products)
}
}
Call the loadPoducts in ViewController:
class MainViewController: UIViewController,UITableViewDataSource, UITableViewDelegate{
#IBOutlet weak var tableView: UITableView!
var products = [Product]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
GetPeoducts.loadProducts(/*some params*/){ (products) in
self.products = products
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.products.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(for: indexPath) as ProductTableViewCell
cell.products = self.products
return cell
}
}
Thank you! #rmaddy #Vanya #Aakash #Jelord Rey
Even though i looked at other threads containing the same question, i have no clue what so ever, why it says this.
My code looks like this:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, HomeModelProtocal {
#IBOutlet weak var listTableView: UITableView!
var feedItems: NSArray = NSArray()
var selectedLocation : Parsexml = Parsexml()
override func viewDidLoad() {
super.viewDidLoad()
//set delegates and initialize homeModel
self.listTableView.delegate = self
self.listTableView.dataSource = self
let homeModel = HomeModel()
homeModel.delegate = self
homeModel.downloadItems()
func itemsDownloaded(items: NSArray) {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of feed items
return feedItems.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cellIdentifier: String = "BasicCell"
let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
let item: Parsexml = feedItems[indexPath.row] as! Parsexml
myCell.textLabel!.text = item.title
return myCell
}
I have tried to use the suggested code, but no luck.
In Swift 3
numberOfRowsInSection is:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
and cellForRowAt indexPath is:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
You can easily find out yourself: Comment out a whole method and retype the first letters tableV and see what code completion suggests.
Even in Swift 2 the signature of cellForRowAtIndexPath is wrong. All parameters are non-optional values.
For a project, I've three UITableViews in an UITabbarController. The initial view loads the tableview correctly, but when I tap on the second tab, the table loads the right amount of cells and the right cell class, but don't show the content on it.
I logged the method tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) and every cell gets the right string values.
This is the code I use:
import UIKit
import RealmSwift
class Hapjes: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tabel: UITableView!
let realm = try! Realm()
let productArray = try! Realm().objects(Product).filter("categorie = 1")
override func viewDidLoad() {
super.viewDidLoad()
tabel.dataSource = self
tabel.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Hapjes"
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ProductenCellHapjes", forIndexPath: indexPath) as! ProductenCell
var object = productArray[indexPath.row]
cell.label.text = object.valueForKey("productNaam") as! String
cell.plaatje.image = UIImage(named: "1449032338_news.png")
let tmp = object.valueForKey("productNaam") as! String
print("Hapje: \(tmp)")
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}
ProductenCell.swift:
import UIKit
class ProductenCell: UITableViewCell {
#IBOutlet weak var plaatje: UIImageView!
#IBOutlet weak var label: UILabel!
}
This is the screenshot of the UI how it goes now:
http://i.stack.imgur.com/Gx0Jw.png
Thanks for your help.
I'm trying to clean up my crowded ViewController by moving the UITableViewDataSource (and Delegate) to a seperate class.
While the DataSource was inside the ViewController (see just below), it worked fine
class ViewController: UIViewController, UITableViewDataSource{
//MARK: Properties
#IBOutlet weak var myTableView: UITableView!
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
override func viewDidLoad()
{
super.viewDidLoad()
myTableView.dataSource = self
}
override func didReceiveMemoryWarning()
{super.didReceiveMemoryWarning()}
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
With The myTableViewCell class
class myTableViewCell: UITableViewCell{
#IBOutlet weak var myLabel: UILabel!
override func awakeFromNib()
{super.awakeFromNib()}
override func setSelected(selected: Bool, animated: Bool)
{super.setSelected(selected, animated: animated)}
}
And this works fine and populates a basic table with the labels filled from the strings in myArray.
However when I move the DataSource to it's own class, as follows, it doesn't work
ViewController
class ViewController: UIViewController
{
//MARK: Properties
#IBOutlet weak var myTableView: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
myTableView.dataSource = myDataSource()
}
override func didReceiveMemoryWarning()
{super.didReceiveMemoryWarning()}
}
and here's the myDataSource class
class myDataSource: NSObject, UITableViewDataSource
{
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
(The myTableViewCell remains the same)
This setup just outputs an empty table, even though all I did was copy-paste the code from the ViewController to myDataSource. What am I missing? (aside form the delegate. I'll deal with that later, first I need to find what's the problem with the data source).
I'm a bit of a rookie to swift, so I'm having a really rough time understanding where I'm going wrong here. Any help at all would be greatly appreciated. If you could just remember in your answer that I'm just starting out, so try not to throw too many complicated concepts at me without explaining. Thanks
Probably the code in your data source never gets called because your data source doesn't get retained by the table here myTableView.dataSource = myDataSource() so basically gets released right away. To solve this, keep the data source as a property.
Declare the separate class as an extension of the ViewController class
extension ViewController: UITableViewDataSource
{
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
I'm using also always delegate methods in an extension declaration but in the same file as the main class