skip to Main Content

I have tableView with a cell of multiple labels. Having cell-like, set-1 variableName1, variablevalue1 & set-2 variableName2, variablevalue2. Here all the data will get from JSON responses including the title label.

I have stored all values to model and showing some data in tableView. but I could not able to show the multiple label values alone.

Here is my JSON structure for multiple labels:

    "_embedded": {
                "variable": [
                    {
                        "_links": {
                            "self": {
                                "href": ""
                            }
                        },
                        "_embedded": null,
                        "name": "startedBy",
                        "value": "Preethi",
                        "type": "String",
                        "valueInfo": {}
                    },
                    {
                        "_links": {
                            "self": {
                                "href": ""
                            }
                        },
                        "_embedded": null,
                        "name": "LoanAmount",
                        "value": "1500000",
                        "type": "String",
                        "valueInfo": {}
                    }
                ]
            },

Here I am trying to show the value from the variable array -> name & value.

Tried so far like this:

       for val in (t._embedded?.variable)!{
        
        print("val name", val.name ?? "")
        print("val name", val.value ?? "")
        

        
        if val.name == val.name {

            cell.variableName1.text = val.name
            cell.variablevalue1.text = val.value
        }


        if val.value == val.value {

            cell.variableName2.text = val.name
            cell.variablevalue2.text = val.value

        }
    }

here the image of the tableView cell:

enter image description here

Any help much appreciated pls…

3

Answers


  1. You can use TableView in CollectionView

    import UIKit
    
    class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    //MARK: - Variable Declaration
    let arryOfData = [["name": "startedBy","value": "Preethi"],["name": "LoanAmount","value": "1500000"]]
    //MARK: - IBOutlet
    @IBOutlet weak var tblView:UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    // MARK: - UITableview DELEGATE AND DATASOURCE
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1 // If you more data than use dynamic count set
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tblView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ListCell
        cell.clcView.reloadData()
        return cell
    }
    
    // MARK: - UICOLLECTION DELEGATE AND DATASOURCE
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.arryOfData.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
        let lblName = cell.viewWithTag(101) as! UILabel
        let lblValue = cell.viewWithTag(102) as! UILabel
        lblName.layer.borderWidth = 1
        lblName.layer.borderColor = UIColor.lightGray.cgColor
        lblValue.layer.borderWidth = 1
        lblValue.layer.borderColor = UIColor.lightGray.cgColor
        let dict = self.arryOfData[indexPath.row]
        lblName.text = dict["name"] ?? ""
        lblValue.text = dict["value"] ?? ""
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let spacing:CGFloat = 10
        let numberOfItemsPerRow:CGFloat = 2
        let spacingBetweenCells:CGFloat = 10
        let totalSpacing = (2 * spacing) + ((numberOfItemsPerRow) * spacingBetweenCells)
        let productWidth = (collectionView.bounds.width - totalSpacing)/numberOfItemsPerRow
        return CGSize(width: productWidth, height: 110)
        
       }
    }
    
    class ListCell: UITableViewCell {
        //MARK: - Variable Declaration
        
        //MARK: - IBOutlet
        @IBOutlet weak var clcView:UICollectionView!
        
        //MARK: - LifeCycle
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
        //MARK: - IBAction
    
     }
    

    your output

    Login or Signup to reply.
  2. for val in (t._embedded?.variable)!{
        
    
        if val.name == val.name {
    
            cell.variableName1.text = val.name
            cell.variablevalue1.text = val.value
        }
    
    
        if val.value == val.value {
    
            cell.variableName2.text = val.name
            cell.variablevalue2.text = val.value
    
        }
    }
    

    If you compare val.name == val.name it always returns true so you need to modify like this val.name == "startedBy" and for value val.value == "1500000" to display the first item and second item of the json.

    Anyway where do you call the for loop?
    I think you want to display each item of the json in a unique cell so you need to parse the json into a dictionary and use it in the "cellForItemAt" function of CollectionViewDataSource

    Login or Signup to reply.
  3. There is little information on what you really want but maybe this can help.

    Case 1

    I feel like your json may not be properly defined. If it is, then it seems to say that you will only have one set of two variables, then you wouldn’t need a UITableView because there would be only one cell, that way you could accomplish that by just adding one UIView to your current view.

    If you:

    • will always have only two variables in the json
    • want to show their content in one cell
    • are using UITableView

    On cellForRow(at:) you can

    func cellForRow(at indexPath: IndexPath) -> UITableViewCell {
    
        guard let cell = self.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) else { return UITableViewCell() }
    
        for val in (t._embedded?.variable)! {
            
            // The if you had here is always true
            cell.variableName1.text = val.name
            cell.variablevalue1.text = val.value
    
            cell.variableName2.text = val.name
            cell.variablevalue2.text = val.value
            }
        }
    
        return cell
    }
    

    Case 2

    If your json is properly defined but you will have more than two variables and you want to show two per row then I would suggest you used UICollectionViewCell and have your cells take half the screen in size each and in each one you show the value of the proper index. Something like this:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath),
              cellInfo = t._embedded?.variable[indexPath.item] else { return UICollectionViewCell() }
    
        cell.variableName.text = cellInfo.name
        cell.variableValue.text = cellInfo.value
    
        return cell
    }
    
    // MARK: Size of the cells
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
        let midWidthPoint = UIScreen.main.bounds.size.width / 2
    
        return CGSize(width: midWidthPoint, height: 100)
    }
    

    Case 3

    If none of these is still the case you want solved please let us know so that we can better help you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search