Nils Wasell
Nils Wasell

Reputation: 997

Accessing custom table cell labels

I have created two custom labels in a table cell in order to be able to dynamically resize the cell(s) to it's content. I first tried using the "Subtitle" style and this worked out great except that the cell(s) didn't resize the way i wanted to and it looked really messy.

My question is: how do I access these labels in order to append my value's from my API to them?

View controller code:

import UIKit

class nyheterViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, APIControllerProtocol {

    @IBOutlet weak var nyheterTableView: UITableView!
    @IBOutlet weak var titleLabel: UILabel!

    var searchResultsData: NSArray = []
    var api: APIController = APIController()

    func JSONAPIResults(results: NSArray) {
        dispatch_async(dispatch_get_main_queue(), {
            self.searchResultsData = results
            print(self.searchResultsData)
            self.nyheterTableView.reloadData()
        })
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        var APIBaseUrl: String = "http://*.se/*/*.php"
        var urlString:String = "\(APIBaseUrl)"

        //Call the API by using the delegate and passing the API url
        self.api.delegate = self
        api.GetAPIResultsAsync(urlString, elementName:"news")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //print(self.searchResultsData.count)
        return self.searchResultsData.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier: String = "nyheterResultsCell"


        let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell

        //nyheterTableViewCell.cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier);

        //Create a variable that will contain the result data array item for each row
        var cellData: NSDictionary = self.searchResultsData[indexPath.row] as NSDictionary
        //Assign and display the Title field
        var releaseDate: String = cellData["date"] as String
        var titleVar: String = cellData["title"] as String
        var titleMix: String = "\(titleVar)" + " - " + "\(releaseDate)"

        cell.textLabel?.text = titleMix //textLabel worked out fine using "Subtitle" style.

        // Get the release date string for display in the subtitle

        cell.detailTextLabel?.text = cellData["content"] as String? //Same

        return cell
    }
}

I understand that I can't access these labels without somehow connecting them to the ViewController. Creating outlets to the ViewController generates an error about that I can't use connections from the prototype cell to the ViewController. So, i created a new class, called nyheterTableViewCell which i connect to the table cell and connected outlets to my labels.

nyhterTableViewCell code:

import UIKit

class nyheterTableViewCell: UITableViewCell {

    @IBOutlet weak var nyhetLabel: UILabel!
    @IBOutlet weak var titleLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

I'm an beginner at Swift-programming and Xcode.

Cheers!

Upvotes: 0

Views: 446

Answers (1)

AdamPro13
AdamPro13

Reputation: 7400

You don't need the labels connected to the view controller. Your example of the custom table view cell looks correct.

To access the label properties, you're going to want to change the line

let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell

to

let cell: nyheterTableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as nyheterTableViewCell

Upvotes: 2

Related Questions