0

I'm making a UITableView in swift programmatically. With the following code (I have UITableViewDelegate and UITableViewDataSource in there):

var tableView = UITableView()
override func viewDidLoad() {
    super.viewDidLoad()

    tableView.frame = CGRectMake(25, 180, 220, 150)
    tableView.delegate = self
    tableView.delegate = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    self.view.addSubview(tableView)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return options.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel?.text = options[indexPath.row]

    println("cell label is")
    println(cell.textLabel!.text)

    return cell
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 30
}

The table is coming blank, however. The cells don't show up. Any advice?

7
  • println("cell label is"), did it print anything? Commented Aug 21, 2015 at 1:30
  • nope it isnt. but the options array is being filled. Commented Aug 21, 2015 at 1:32
  • It means options.count has problem. Can you try print options.count int ? Commented Aug 21, 2015 at 1:34
  • Do you want options.count sections? and 1 row? func numberOfSectionsInTableView(tableView: UITableView) -> Int { return options.count } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } Commented Aug 21, 2015 at 1:35
  • Why tableView.delegate initialized twice in viewDidLoad?One of them must be datasource isn't it? Commented Aug 21, 2015 at 1:36

4 Answers 4

1

Why tableView.delegate initialized twice in viewDidLoad?One of them must be datasource isn't it?

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.frame = CGRectMake(25, 180, 220, 150)
    tableView.delegate = self
    tableView.datasource = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    self.view.addSubview(tableView)
}
Sign up to request clarification or add additional context in comments.

Comments

0

Call tableView.reloadData() after adding it to self.view in viewDidLoad.

4 Comments

Print your options.count to see if you have data to show.
If you have only 1 row per section why use section? Move return options.count to numberOfRowsInSection and delete numberOfSectionsInTableView (the default is 1)
the options array is being filled. i print it after i query. right after that, i do tableView.reloadData()
Post the actual code please, there is no print in the one you posted. Are your delegates called?
0

Not only you are missing self.tableView.reloadData() . What do you have inside of the data structure options where are you getting the data from?

you said you are Querying from parse :

 var query = PFQuery(className: "whatever class name")
    query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
        if error == nil {

            if let newObjects = objects as? [PFObject]{

                for one in newObjects {


                    var message = one["TextMessage"] as! String
                    // so add this data message data into your array 
                    // then reload Data
                }

            }

        }
        else{
           print("error")
        }
    }
}

And also change this

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

To

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return options.count 
 }

4 Comments

I'm querying my database (using Parse), for them using a synchronous query. I have checked and the options are loading fine. After the query, I'm doing tableView.reloadData()
i do not think parse code is the issue, as the query is populating the options array. i am printing that array and seeing that it is populated. after i query parse and the array is filled, i do tableView.reloadData() so the issue has to be something else
ok read the last part of my edit you have mixed up the row method with the section method
good call on the changing numberofrowsinsection thing. that was a typo on my part. but the issue still persists :/
0

I noticed that you (unconventionally) use each element in options as i row in its own section. But, when you set the label, you base it on the row number, which will alway be zero. What you want is: cell.textLabel?.text = options[indexPath.section]

As for the table being blank, I would think that either

  1. Your first element is nothing
  2. The array is empty
  3. You are not setting up the sections properly

Comments

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.