This is a sample code with UITableViewController and CoreData. Main file MainTableViewController.swift:
import UIKit
import CoreData
class MainTableViewController: UITableViewController {
var results:AddrBook[]=[]
init(style: UITableViewStyle) {
super.init(style: style)
}
init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidAppear(animated: Bool) {
let request = NSFetchRequest(entityName: "Person")
request.returnsObjectsAsFaults = false
let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context:NSManagedObjectContext = appDelegate.managedObjectContext
results = context.executeFetchRequest(request, error: nil) as AddrBook[]
self.tableView.reloadData()
}
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return results.count
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
if !cell {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
cell!.textLabel.text = results[indexPath.row].lastname + " " + results[indexPath.row].firstname
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject?) {
var indexPath = self.tableView.indexPathForSelectedRow()
let destViewController:DetailViewController! = segue.destinationViewController as DetailViewController
if segue.identifier == "editPerson" {
destViewController.receivedPerson = results
destViewController.indexPath = indexPath
}
}
}
If in cellForRowAtIndexPath I use this:
cell!.textLabel.text = results[indexPath.row].lastname + " " + results[indexPath.row].firstname
then all is good. But if I use this:
cell!.textLabel.text = results[indexPath.row].lastname
cell!.detailTextLabel.text = results[indexPath.row].firstname
I see error: Can't unwrap Optional.None
What's wrong? Help, please.
Just in case the codes of other classes
Class UIViewController for add and edit records (DetailViewController.swift):
import UIKit
import CoreData
class DetailViewController: UIViewController {
#IBOutlet var currentOperation : UILabel = nil
#IBOutlet var firstnameField : UITextField = nil
#IBOutlet var lastnameField : UITextField = nil
var indexPath = NSIndexPath()
var receivedPerson:AddrBook[]=[]
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
if !receivedPerson.isEmpty { // If selected row in tableview in MainTableViewController
currentOperation.text = "Edit Person"
firstnameField.text = receivedPerson[indexPath.row].firstname
lastnameField.text = receivedPerson[indexPath.row].lastname
}
else { // If pressed "+" in MainTableViewController
currentOperation.text = "Add Person"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func saveButton(sender : AnyObject) {
let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context:NSManagedObjectContext = appDelegate.managedObjectContext
if receivedPerson.isEmpty { // If pressed "+" in MainTableViewController
let projectEntity = NSEntityDescription.entityForName("Person", inManagedObjectContext: context)
var newPerson = AddrBook(entity: projectEntity, insertIntoManagedObjectContext: context)
newPerson.lastname = lastnameField.text
newPerson.firstname = firstnameField.text
}
else { // If selected row in tableview in MainTableViewController
receivedPerson[indexPath.row].firstname = firstnameField.text
receivedPerson[indexPath.row].lastname = lastnameField.text
}
context.save(nil)
self.navigationController.popViewControllerAnimated(true)
}
}
Class AddrBook.swift for CoreData:
import UIKit
import CoreData
#objc(AddrBook)
class AddrBook: NSManagedObject {
#NSManaged var lastname:String
#NSManaged var firstname:String
}
Use
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
instead
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
Swift 4+
Use
cell = UITableViewCell(style: .value1, reuseIdentifier: "Cell")
instead
cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
For Swift 4+ , use:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
if( !(cell != nil))
{
cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
}
cell!.textLabel?.text = "Hello"
return cell!
}
try using like this.
just avoid the exclamation marks
cell.text = results[indexPath.row].lastname as NSString
cell.detailTextLabel.text = results[indexPath.row].firstname as NSString
And i see that , there is lots error in your code. !!
Related
I am trying to show an object from firebase in a custom cell. It has three text labels. When, I run my code it only shows one line of text per cell, instead of three, in the tableview. It only returns which ever text label is first.
Here is my code for my Class object:
class Class: NSObject {
var date_clasname: String?
var teacher: String?
var room_number: String?
init(dictionary: [String: Any]) {
self.date_clasname = dictionary["date_clasname"] as? String ?? ""
self.teacher = dictionary["teacher"] as? String ?? ""
self.room_number = dictionary["room_number"] as? String ?? ""
}
}
Here is my code for my tableview class:
class classes_test_TableViewController: UITableViewController {
let cellId = "cellId"
var users = [Class]()
override func viewDidLoad() {
super.viewDidLoad()
//navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel))
tableView.register(UserCell.self, forCellReuseIdentifier: cellId)
fetchClass()
}
func fetchClass() {
// guard let uid = ?.user.uid
// else{return}
//let userID = Auth.auth().currentUser!.uid
Database.database().reference().child("Science").observe(.childAdded, with: { (snapshot) in
//print(userID)
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = Class(dictionary: dictionary)
self.users.append(user)
print(snapshot)
//this will crash because of background thread, so lets use dispatch_async to fix
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
}, withCancel: nil)
}
#objc func handleCancel() {
dismiss(animated: true, completion: nil)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let use a hack for now, we actually need to dequeue our cells for memory efficiency
// let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
let Class = users[indexPath.row]
cell.textLabel?.text = Class.date_clasname
cell.textLabel?.text = Class.teacher
cell.textLabel?.text = Class.room_number
return cell
}
}
class UserCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier:
String?) {
super.init(style: .default, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Here is my database structure:
"Science" : {
"-Lgxm6qJhzI2IIG4uary" : {
"date_clasname" : "f",
"room_number" : "d",
"teacher" : "f"
}
The cell is suppose to show all three strings but only shows one.
You are using the standard UITableViewCell and you assign all three values to the same label.
You have to cast the cell to the custom cell and assign the values to the custom labels
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let use a hack for now, we actually need to dequeue our cells for memory efficiency
// let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell
let user = users[indexPath.row]
cell.nameLabel?.text = user.date_clasname
cell.teacherLabel?.text = user.teacher
cell.roomLabel?.text = user.room_number
return cell
}
Replace nameLabel, teacherLabel and roomLabel with the real property names.
And please conform to the naming convention and name variables lowerCamelCased for example dateClasname and roomNumber
I'm trying to remove a row if the condition retrieved from var cellStatus = object["active"] as! Bool is false. I've tried a couple of different things and cant seems to get anything to work. Hiding the cell just leaves a large gap in the tableView.
class TableViewController: PFQueryTableViewController {
override init(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.parseClassName = "specials"
self.pullToRefreshEnabled = true
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
var query = PFQuery(className: parseClassName!)
query.limit = 6
query.orderByAscending("specialRank")
return query
}
//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject!) -> PFTableViewCell {
var cellStatus = object["active"] as! Bool
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
if let locationName = object["locName"] as? String {
cell?.textLabel?.text = locationName
}
if let spec = object["special"] as? String {
cell?.detailTextLabel?.text = spec
}
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
var detailScene = segue.destinationViewController as! DrinkInfoViewController
// Pass the selected object to the destination view controller.
if let indexPath = self.tableView.indexPathForSelectedRow() {
let row = Int(indexPath.row)
detailScene.currentObject = objects?[row] as? PFObject!
}
}
override func viewWillAppear(animated: Bool)
{
self.navigationController?.navigationBarHidden = false
}
}
Im using PFQueryTableViewController to load all my data from parse.
Currently it loads all the data at once, so in the future if i have many data it will mess up the user experience of users. How do i load it 5 per 5 whenever I scrolled down?
Here's the code
import UIKit
import Parse
import ParseUI
class MainViewTable: PFQueryTableViewController {
// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.parseClassName = "product"
self.textKey = "createdAt"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "product")
query.orderByDescending("createdAt")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell!
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
}
if let titleName = object?["title"] as? String {
cell.title.text = titleName
}
if let priceTitle = object?["price"] as? Int {
cell.price.text = String(priceTitle)
println(cell.price.text! + "Price")
}
return cell
}
override func viewDidAppear(animated: Bool) {
// Refresh the table to ensure any data changes are displayed
tableView.reloadData()
}
}
I am trying to get videos that i saved to parse to show up in my tableview cell when i load the data but for some reason when i run the program the table view is blank. I have tried using UITableViewController instead of parse's but i couldn't get it to work that way either. Is there any way i can get the video to show up in the tableview cell?
Code:
import UIKit
import MediaPlayer
class TableViewController: PFQueryTableViewController {
var song: NSURL!
var player:MPMoviePlayerController!
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.parseClassName = "Videos"
self.pullToRefreshEnabled = true
self.objectsPerPage = 10
self.paginationEnabled = true
}
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: self.parseClassName)
if (objects.count == 0)
{
query.cachePolicy = kPFCachePolicyNetworkOnly
}
return query
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject! {
var obj : PFObject? = nil
if(indexPath.row < self.objects.count){
obj = self.objects[indexPath.row] as PFObject
}
return obj
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as PFTableViewCell
let video:PFFile = object["musicVideo"] as PFFile
let url: NSURL = NSURL(string: video.url)!
println(video)
player = MPMoviePlayerController(contentURL: url)
player.controlStyle = MPMovieControlStyle.None
player.scalingMode = MPMovieScalingMode.AspectFit
player.view.frame = cell.contentView.bounds
cell.contentView.addSubview(player.view)
player.view.hidden = false
player.prepareToPlay()
player.play()
return cell
}
}
NB the correct code for setting the objects in a PFQueryTableViewController in Swift 1.2 appears to have changed slightly (force unwrapping to PFObject! will cause a crash when the last cell in the table is loaded, if it is not the "load more" cell, as will counting objects! instead of objects?).
override func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject? {
var obj : PFObject? = nil
if(indexPath.row < self.objects!.count){
obj = self.objects?[indexPath.row] as? PFObject
}
return obj
}
All I have added this into my appdelegate
var controller:PFQueryTableViewController = PFQueryTableViewController(className: "Types_")
self.window?.rootViewController = controller
println(self.window?.rootViewController)
I have created an new class in swift like this :
class TableViewController: PFQueryTableViewController {
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.parseClassName = "Types_"
self.pullToRefreshEnabled = true
self.paginationEnabled = true
self.objectsPerPage = 5
//self.textKey = "TypeOfVenue_"
}
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: self.parseClassName)
if (objects.count == 0)
{
query.cachePolicy = kPFCachePolicyNetworkOnly
}
return query
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell!
{
var cellIdentifier = "eventCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)
}
cell.textLabel?.text = object["TypeOfVenue_"] as NSString
cell.detailTextLabel?.text = object["Seating"] as NSString
return cell
}
But the new class does not execute, if i add a breakpoint in the class, it doesn't execute the breakpoint.
I have added a ViewController in the Storyboard, and linked the class to the view controller like this :
Anyone have any ideas ?
I created a TableView controller on my Storyboard, and removed the code in the app delegate to create the controller.
Then I added this class as the the class on the tableview controller, and it worked.