0

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...

2
  • you can use tableview in collection-view. tableview section in variable.count and tableview number of rows static return 1 after collection reload. collection view number of rows -> variable.count after collection view cell for indexpath in two labels in one for name and one for value...i think my logic working. now i have to create one for demo end send to you. Commented Dec 23, 2020 at 7:15
  • expire your bounty +50 please accept my answer otherwise you lost +50 bounty point. Commented Jan 8, 2021 at 4:39

3 Answers 3

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

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

3 Comments

Thanks for your suggestions. Thought doing this already. do we have other solutions?
How does this solution not solve your problem? If you are already doing this, what do you want? Perhaps provide more of the actual code you're using along with more details about how you've structured your app.
@PvUIDev second solution is you can create custom arraryofdictionary and than show only table view.
0
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

Comments

0

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.

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.