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.
Now you can select your red view and Xcode will indicate which view is selected.
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!
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:
- Set
estimatedRowHeight
to whatever you think the size will be. It doesn't have to be exact, but it helps with performance. - Set
rowHeight
toUITableView.automaticDimension
. - Set
top
&bottom
constraints of theview
as well as aheight
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
Order Two Nsmutablearrays Based on One
Is It Ok to Place Most Logic and Models in the Appdelegate
Invalid Device State - Xcode/iOS Simulator Error
Swift Invalidate Timer Doesn't Work
How to Get a Swift Type Name as a String with Its Namespace (Or Framework Name)
Swift Optional Chaining Doesn't Work in Closure
Detect App Launch from Widgetkit Widget Extension
Prevent Dispatch_After() Background Task from Being Executed
How to Use Unsafemutablepointer in Swift 3
Responding to Ram Availability in iOS
Objective-C Wrapper for Cfunctionpointer to a Swift Closure
Swift - Protocol Extensions - Property Default Values