in Education by
I am creating an UITableView using auto layout and prototype cells. I would like it to act more like a web HTML table. Each cell has four "columns", each with one label in it, see here. Ultimately, each column should adjust its width to the widest column of all the other rows. This seems like a common case but I am unable to find any info about it. Am I missing something obvious here? What result I have right now: 1|product name|11 xx|22 yy 1|longer product name|11 xxx|22 yy 1|short|1x|22 yy What I would like: 1|product name |11 xx |22 yy 1|longer product name|11 xxx|22 yy 1|short |1x |22 yy I hope this makes any sense, otherwise let me know. Thanks! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can build a "grid" of labels using embedded UIStackViews Use an "outer" horizontal stack view to hold 4 "inner" stack views, which are used as the 4 "columns". Embed the outer stack view in a UIScrollView in case the content exceeds the available area. The result looks like this: The first two "column" stack views have .alignment = .leading and the 3rd and 4th have .alignment = .trailing. As you can see, each "column" stack view stretches its width to fit the widest label. Here is some code to produce that example: class StackGridViewController: UIViewController { var myData = [[String]]() override func viewDidLoad() { super.viewDidLoad() // init 30 rows of data for i in 1...30 { myData.append(["\(i)", "Product Name", "12 xx", "34 xx"]) } // make a few of the rows different for i in [2, 15, 21] { // longer "column B" myData[i][1] = "Longer Product Name" // longer "column C" myData[i+2][2] = "1234 xx" // longer "column D" myData[i+4][3] = "3456 xx" } // and one row with the longest values // longest "column B" myData[9][1] = "The Longest Product Name" // longest "column C" myData[9][2] = "123456 xx" // longest "column D" myData[9][3] = "345678 xx" // create a horizontal stack view let outerSV = UIStackView() outerSV.translatesAutoresizingMaskIntoConstraints = false outerSV.axis = .horizontal outerSV.distribution = .fill // "column" spacing outerSV.spacing = 12 // add 4 "column" vertical stack views for col in 0..<4 { let columnSV = UIStackView() columnSV.translatesAutoresizingMaskIntoConstraints = false columnSV.axis = .vertical // align leading for first two columns, trailing for 3rd and 4th columnSV.alignment = col < 2 ? .leading : .trailing columnSV.distribution = .fill // "row" spacing columnSV.spacing = 8 // add a label for each data "row" for row in 0..<myData.count { let v = UILabel() v.backgroundColor = UIColor(white: 0.9, alpha: 1.0) v.font = UIFont.systemFont(ofSize: 13.0, weight: .light) v.text = myData[row][col] columnSV.addArrangedSubview(v) } // add the "column" stack view to the outer stack view outerSV.addArrangedSubview(columnSV) } // create a scroll view let scrollView = UIScrollView() scrollView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(scrollView) // constrain it to all four sides with 20-pt padding on top/bottom, 8-pt padding leading/trailing NSLayoutConstraint.activate([ scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20.0), scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20.0), scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 8.0), scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -8.0), ]) // add the outer stack view to the scroll view scrollView.addSubview(outerSV) // constrain it to all four sides (which will also define .contentSize (the "scrollable area") NSLayoutConstraint.activate([ outerSV.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0.0), outerSV.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0.0), outerSV.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0.0), outerSV.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0.0), ]) } }

Related questions

0 votes
    I have a UICollection that shows a padlock on cells that locked to users who aren't logged in. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    Turning on the rulers or gridlines that are built into most of the software programs helps in aligning Elements. (1)False (2)True...
asked May 18, 2021 in Technology by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    I have a UITableView inside a UITableViewCell (Scrolling is disabled for the nested UITableView) Everything works ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    Hello I am using native UITableViewCell in my UITableView. When I set the image of the cell it looks ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    I have a UITextField inside of a UITableView cell. When I rotate the device, the textfield is still the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I created an LSTM with Keras API. Now I am facing an issue while I try to test different values ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I have a 9-cell grid containing images (using bootstrap columns). The grid works fine and is responsive ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I'd like to add a CMS and blog to a web app. One that won't get in the way. There's ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    I am currently using numpy.polyfit(x,y,deg) to fit a polynomial to experimental data. I would however ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    ________ makes it incredibly easy to fit time series models like ARIMA, ARMA, AR, Exponential Smoothing, etc. (a ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    In practice, Line of best fit or regression line is found when _____________ (a) Sum of residuals (∑(Y ... Regression of R Programming Select the correct answer from above options...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    To fit a graphic or clip art on to a slide you can change the (a) Design template (b) Slide layout (c) Colour scheme (d) Outline Select the correct answer from above options...
asked Dec 11, 2021 in Education by JackTerrance
0 votes
    Complete the sentence by choosing TWO WORDS either of which will fit the blank. The two words must both produce a ... on his bike. Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
...