How to display Data in 2 columns in Table dynamically

Hi


I want to display below Data in table Dynamically. No of records can vary.


Title Desc

Title Desc

Title Desc


{
    error = 0;
    message = Ok;
    result =     (
                {
            id = 81;
            "desc" = "<p>Traveler can select any date of travel before 60 days . He can pay remaining payment as per the payment plan mentioned above. </p>
\n";
            title = "Payment Policy";
        },
                {
            id = 76;
            "desc" = "<p> </p>
\n
\n<p>6000</p>
\n";
            title = Advance;
        },
                {
            id = 68;
            "desc" = "<p>25% of Tour Cost (including booking amount)</p>
\n";
            title = "On confirmation of Price quote";
        },
                {
            id = 79;
            "desc" = "<p>70% of Tour Cost</p>
\n";
            title = “30 Days Before Departure";
        },
                {
            id = 77;
            "desc" = "<p>Total Cost</p>
\n";
            title = "10 Days before Departure";
        }
    );
}


Thanks

So, what is your problem ?


- create a tableView with 2 columns

- create arrays for data sources (or a dictionary if id is unique)

- use them in the tableview(displayAtPath:) function

Hi


I am creating table like this . How i can give height dynamicaaly according to the content. Secondly i am displaying Data like this . Can i create Borders and separate columns with border.


self.tableView.frame = CGRect(x: 10, y: 1500, width: 200, height: 500)
                    self.tableView.dataSource = self
                    self.tableView.delegate = self
                    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
                    self.view.addSubview(self.tableView


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "td")
       
        let dict = array1.object(at: indexPath.row) as! NSDictionary
       
        let firstValue = dict.value(forKey: "title") as! String
        let secondValue = dict.value(forKey: "desc") as! String

        cell.textLabel?.text = firstValue
        cell.detailTextLabel?.text = secondValue
        return cell
    }

Thanks

Why don't you create the table in IB ?


How is array1 defined (to check if let dict = array1.object(at: indexPath.row) as! NSDictionary is correct) ?


Take care with names which are confusing.

In fact, dict is for one Cell, not the full dictionary ; you'd probably be better off calling it dictItem or dictContentForCell


Personally, I would do a bit differently :

have a global property in the controller :

        var dictForTable = [Int: (title: String, descr: String)]()

Int will be the row, with the content of title and desc for each

You populate at creation, because you say you will not add new cells.

dictForTable[0] = (titleOfCell0: descrOfCell0) // etc…


then in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)


        let firstValue = dictForTable[indexPath.row]!.title 
        let secondValue = dictForTable[indexPath.row]!.descr


for safety, you could test for nil

       if  let firstValue = dictForTable[indexPath.row].title,  let secondValue = dictForTable[indexPath.row].descr {
        cell.textLabel?.text = firstValue
        cell.detailTextLabel?.text = secondValue
     } else {
        cell.textLabel?.text = ""
        cell.detailTextLabel?.text = ""
}
How to display Data in 2 columns in Table dynamically
 
 
Q