0

I am trying to add parse data for an "Announcements" page on my app. I have set up the pages ViewController, Added the TableView, and followed the steps to get it to accurately print the correct amount of rows. The issue is the text itself. I am trying to connect it to a UILabel for the Header but its not working. Any help is appreciated.

import UIKit
import Parse
import Bolts

class ViewController: UIViewController, UITableViewDelegate {

@IBOutlet var tableView: UITableView!

var Header = [String]()




let reuseIdentifier = "ContentCell"
private let cellHeight: CGFloat = 210
private let cellSpacing: CGFloat = 20

@IBOutlet var barButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    let navBar = self.navigationController!.navigationBar
    navBar.barTintColor = UIColor(red: 6.0 / 255.0, green: 100.0 / 255.0, blue: 255.0 / 255.0, alpha: 1)
    navBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

    var query = PFQuery(className: "Announcements")
    query.orderByDescending("createdAt")
    query.findObjectsInBackgroundWithBlock {
        (posts:[PFObject]?, error: NSError?) -> Void in

        if error == nil {
            //success fetching announcements

            for alert in posts! {
                print(alert)
            self.Header.append(alert["Header"] as! String)
            }

            /***Reload The Table***/

            print(self.Header.count)

            self.tableView.reloadData()

        } else {
            print(error)

        }
}

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

    return Header.count
    //turns announcements into rows

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let singleCell: AnnouncementCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("AnnouncementBlock") as! AnnouncementCellTableViewCell

    singleCell.HeaderText.text = Header[indexPath.row]

    return singleCell


}
7
  • 1. Are you sure HeaderText is connected with an outlet to the label in the view? 2.you should use tableView.dequeueReusableCellWithIdentifier("AnnouncementBlock", forIndexPath: indexPath) as it won't return nil on you Commented Oct 28, 2015 at 1:19
  • 3. Storyboard or xib? Commented Oct 28, 2015 at 1:23
  • @Animal are you referring to the main view or the "announcementCellTableViewCell" view? . I Changed the dequeureusablecellwithidentifier but it didnt work. Should it be "let singleCell: AnnouncementCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("AnnouncementBlock", forIndexPath: indexPath) as! AnnouncementCellTableViewCell" Commented Oct 28, 2015 at 1:40
  • And its in Storyboard format Commented Oct 28, 2015 at 1:40
  • The deque is not the problem here but you get into the habit of using it. Otherwise you have to check if your cell is nil... It's not related to the current problem though. Commented Oct 28, 2015 at 1:50

3 Answers 3

1

I saw that you have declared a variable

let reuseIdentifier = "ContentCell"

Are you using the right cell?

If you are using right cell and have properly connected HeaderText label outlet with AnnouncementCellTableViewCell (make sure it is connected with the cell not view) and you are getting the value from Parse for Header then this should work:

func tableView(tableView: UITableView, cellForRowAtIndexPath     indexPath: NSIndexPath) -> UITableViewCell{
      let cell = tableView.dequeueReusableCellWithIdentifier("AnnouncementBlock", forIndexPath: indexPath) as! AnnouncementCellTableViewCell
      cell.HeaderText.text = Header[indexPath.row]
      return cell
 }

Hope this Help.

Sign up to request clarification or add additional context in comments.

1 Comment

Does anything have to be in my AnnouncementCellTableViewCell.swift file?
0

If you are using a custom cell made in the tableView in a story board I think the easiest way to "hoko up" the label is to give it a tag number in the properties window in storyboard.

then in you cellForRowAtIndexPath you fetch the label with the tag

if let label = cell.viewWithTag(tag: tagNumber) as? UILabel {
    label.text = Header[indexPath.row]
}

1 Comment

That's not much of a clue. What errors? What kind of errors? When you run the code or compilation errors?
0

The code needs to call registerClass:forCellReuseIdentifier: on tableView for your custom cell class. To dequeue the cell, use dequeueReusableCellWithIdentifier:indexPath:

// in or around viewDidLoad()
self.tableView.registerClass(AnnouncementCellTableViewCell.self, forCellReuseIdentifier: "AnnouncementBlock")

// in cellForRowAtIndexPath
let singleCell = tableView.dequeueReusableCellWithIdentifier("AnnouncementBlock", forIndexPath:indexPath) as AnnouncementCellTableViewCell

2 Comments

Does anything have to be in my AnnouncementCellTableViewCell.swift file?
Nothing required, but it's a good place to put logic that configures the cell given an element of the model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.