SaiPavanParanam
SaiPavanParanam

Reputation: 416

Repeat a label in TableViewCell

Please find the below screenshot.

Mock up I need the solution to be working in iOS 8 too. Stack View is not helpful in iOS 8

I have been assigned this mock up... I don't exactly know how to repeat a particular label in the tableViewCell. As you can see I need to repeat the violet label and white labels depending on the items in JSON data..

Upvotes: 1

Views: 1050

Answers (5)

Mike Taverne
Mike Taverne

Reputation: 9352

You can use a combination of horizontal and vertical stack views to achieve this:

enter image description here

This solution allows you to avoid two pitfalls with the other approaches suggested above:

  • You don't have to mess with toggling views' hidden property
  • You aren't limited to a fixed maximum number of items

Here's the code:

import UIKit

class Order {
    var stations = [Station]()
    var companies = [String]()
}
class Station {
    let name: String
    let amount: Int
    init(name: String, amount: Int) {
        self.name = name
        self.amount = amount
    }
}

class TableViewController: UITableViewController {

    var orders = [Order]()

    override func viewDidLoad() {
    super.viewDidLoad()

    tableView!.rowHeight = UITableViewAutomaticDimension
    tableView!.estimatedRowHeight = 44.0

    //simulated data
    let order1 = Order()
    order1.companies.append("Global @ CITGO Braintree, Braintree")
    order1.companies.append("Exxon @ Global, Chelsea")
    order1.stations.append(Station(name: "Tyngsboro Mobil #2871", amount: 4000))
    order1.stations.append(Station(name: "Grafton St #10095", amount: 15000))
    orders.append(order1)

    let order2 = Order()
    order2.companies.append("Exxon @ Global, Chelsea")
    order2.stations.append(Station(name: "Winstead Citgo #2001", amount: 3000))
    orders.append(order2)

    let order3 = Order()
    order3.companies.append("MSCG @ Sunoco Partners")
    order3.stations.append(Station(name: "Citgo #123", amount: 7000))
    order3.stations.append(Station(name: "Mobil #345", amount: 12500))
    order3.stations.append(Station(name: "Exxon #567", amount: 300))
    order3.stations.append(Station(name: "Citgo #789", amount: 6100))
    order3.stations.append(Station(name: "Stoughton Mobil #2744", amount: 9098))
    order3.stations.append(Station(name: "Westborough Mobil #2720", amount: 120000))
    orders.append(order3)
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as! TableViewCell

    let order = orders[indexPath.row]

    cell.companyLabel.text = ""
    for (index, company) in order.companies.enumerated() {
        cell.companyLabel.text! += company
        if index < order.companies.count - 1 {
            cell.companyLabel.text! += "\n"
        }
    }

    for station in order.stations {

        // create horizontal stack view to arrange the station name and amount
        let horizontalStackView = UIStackView()
        horizontalStackView.axis = .horizontal

        let nameLabel = UILabel()
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        nameLabel.text = station.name
        horizontalStackView.addArrangedSubview(nameLabel)

        let amountLabel = UILabel()
        amountLabel.translatesAutoresizingMaskIntoConstraints = false
        amountLabel.addConstraint(NSLayoutConstraint(item: amountLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 60))
        amountLabel.backgroundColor = UIColor.purple
        amountLabel.textAlignment = .right
        amountLabel.textColor = UIColor.white
        amountLabel.text = String(station.amount)
        horizontalStackView.addArrangedSubview(amountLabel)

        // add the horizontal stack view to the vertical stack view containing all stations
        cell.stackView.addArrangedSubview(horizontalStackView)
    }

    return cell
}

}

Here's what the storyboard looks like:

enter image description here

Upvotes: 1

Radim Halfar
Radim Halfar

Reputation: 536

As Mr.Bista mentioned there is an option with StackView.

  1. Create custom cell in Storyboard with the design.
  2. Instead of adding multiple labels use stack view.
  3. In code use self sizing cells. You can refer to this site to guide you. APPCode self sizing cells or just seek on stackoverflow.

I did not test it by myself now, but I assume it can be tricky to use stackView and self sizing cell. Since every cell can have different height based on contents of StackView. So to be hundred percent sure, you can always calculate the height of cell.

  1. Change the content of stack view based on json content. (Number of records for violet labels)

You can also check the stack for similar questions. Please see this link where there is possible answer for you. Multiple UILabels inside a self sizing UITableViewCell

Upvotes: 1

Олег Місько
Олег Місько

Reputation: 370

According to @the_dahiya_boy answer:

  1. You get the JSON field with specific fields (there can be one or two fields that need to be displayed in violet fields)

  2. You check how many fields there are

  3. If there is only one field then you can take set the frame of your second view (for second field, that is empty) as 0

Upvotes: 0

Bista
Bista

Reputation: 7903

This is the perfect scenario to user UIStackView, add 5 labels inside Stack View and hide(.isHidden Property) the labels which you do not want to show, this will cause Stack View to Shrink and Expand based upon number of labels present inside it.

I hope you are already informed with Stack View, if not then you must look for basic online tutorial.

Upvotes: 2

dahiya_boy
dahiya_boy

Reputation: 9503

In storyboard you have to two UILabel each for white and violet. If Json have data then assign it otherwise do below code

cell.label1.frame.size.height = 0
cell.label2.frame.size.height = 0

Note : Becarefull about constraints you are giving to the components in the UITableViewCell.

Upvotes: 0

Related Questions