Uicellview Cell Layout in Swift

UICellView cell layout in swift

Simple, add an extension of your view controller that implements the UICollectionViewDelegateFlowLayout protocol:

extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let size = collectionView.frame.size.width / CGFloat(cols) - CGFloat((cols - 1)) * spacingBetweenCells
return CGSize(width: size, height: size)
}
}

spacingBetweenCells represents here the spacing you want to place between your cells.

Swift UICollectionView in a UITableViewCell dynamic layout can't change cell height

You can Use View Debugging

Run the project, go back to Xcode and click on the Debug View Hierarchy button in the Debug bar. Alternatively, go to Debug\View Debugging\Capture View Hierarchy.

enter image description here

Now you can select your red view and Xcode will indicate which view is selected.

enter image description here

For more info. Check out: View Debugging in Xcode

Different UITableViewCell subviews layout after tapping a cell

I'd have different cells and reload them as the cells are clicked.

class TableViewController: UITableViewController {

var largeRow: IndexPath?

var data: [Int] = [0, 1, 2, 3, 4, 5, 6, 7]

override func viewDidLoad() {
super.viewDidLoad()
}
}

extension TableViewController {

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

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath == largeRow {
return makeLargeCell(at: indexPath)
} else {
return makeNormalCell(at: indexPath)
}
}
}

extension TableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)

if let largeRow = largeRow {
if largeRow == indexPath {
self.largeRow = nil
tableView.reloadRows(at: [indexPath], with: .fade)
} else {
self.largeRow = indexPath
tableView.reloadRows(at: [largeRow, indexPath], with: .fade)
}
} else {
self.largeRow = indexPath
tableView.reloadRows(at: [indexPath], with: .fade)
}
}
}

Auto-sizing UITableViewCell which contains UICollectionView

First of all thanks to everybody for their responses!

After two days of pulling out my hair, it looks like UITableViewCell layout not updating until cell is reused solved it for me.

The problem was the missing

    cell.layoutIfNeeded()

at the end of

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

Here you can see what it looks like now. The setup is like in the original question.

Again thanks everybody for your pointers!

Here you can see the result

Reuse layout code for a UITableViewCell of similar layout

Make a function that just takes the UIViews as parameters and creates the layout.

Then make a base class that derives from table cell and move that function there.

Finally change this class to derive from the new base class

Now your new cell can also derive from that class as well.

Auto layout with custom UITableViewCell without storyboard programmatically (Swift 5)

To get automatic sizing, you need to do 3 things:

  1. Set estimatedRowHeight to whatever you think the size will be. It doesn't have to be exact, but it helps with performance.
  2. Set rowHeight to UITableView.automaticDimension.
  3. Set top & bottom constraints of the view as well as a height constraint.

UITableViewCell frame margins in Swift

For cells, try this:

override func tableView(tableView: UITableView, 
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
cell.separatorInset = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero
}

But based on the edited information, it seems that you have margin in the container view of the table view. Either this comes from your constraints between table view and view (check them one by one) or you have layout margins in the view, in which case try;

 override func viewDidLoad() {
// ...
self.view.layoutMargins = UIEdgeInsetsZero
}


Related Topics



Leave a reply



Submit