Swift 3 incorrect initial UITableViewCell row height - ios

I am trying to programmatically design the layout of my custom UITableView cells. However, I get some strange behavior: despite setting the rowHeight, the first four rows are the default height, and then the rest are what I specified. This impacts the design of my cell, because I programmatically lay out labels based in part on the row height.
I initialize the tableview as follows:
func gameTableInit() {
gameTableView = UITableView()
gameTableView.delegate = self
gameTableView.dataSource = self
let navFrame = self.navigationController?.view.frame
gameTableView.frame = CGRect(x: navFrame!.minX, y: navFrame!.maxY, width: self.view.frame.width, height: self.view.frame.height - navFrame!.height - (self.tabBarController?.view.frame.height)!)
gameTableView.rowHeight = gameTableView.frame.height/4 //HEIGHT OF TABLEVIEW CELL
gameTableView.register(GameCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(gameTableView)
}
This is my cellForRowAtIndexPath function:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! GameCell
print("height of cell: \(cell.frame.height)")
var game = Game()
game.awayTeam = "CLE"
game.homeTeam = "GSW"
cell.setUIFromGame(g: game)
return cell
}
The print statement prints:
height of cell: 44.0
height of cell: 44.0
height of cell: 44.0
height of cell: 44.0
After some scrolling, it then prints the expected:
height of cell: 166.75
...
I have created a custom tableview cell called GameCell
class GameCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
override func layoutSubviews() {
super.layoutSubviews()
positionUIElements()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//on left
var awayTeamAbbrev = UILabel()
var awayTeamLogo: UIImage!
//on right
var homeTeamAbbrev = UILabel()
var homeTeamLogo: UIImage!
func positionUIElements() {
//away team abbreviation is centered on upper left half of cell
awayTeamAbbrev.frame = CGRect(x: self.frame.minX, y: self.frame.minY, width: self.frame.midX, height: self.frame.height/4)
awayTeamAbbrev.textAlignment = .center
awayTeamAbbrev.adjustsFontSizeToFitWidth = true
//awayTeamLogo
//home team abbreviation is centered on upper right half of cell
homeTeamAbbrev.frame = CGRect(x: self.frame.midX, y: self.frame.minY, width: self.frame.midX, height: self.frame.height/4)
homeTeamAbbrev.textAlignment = .center
homeTeamAbbrev.adjustsFontSizeToFitWidth = true
//homeTeamLogo
self.contentView.addSubview(awayTeamAbbrev)
self.contentView.addSubview(homeTeamAbbrev)
}
func setUIFromGame(g: Game) {
awayTeamAbbrev.text = g.awayTeam!
homeTeamAbbrev.text = g.homeTeam!
}
}
I have tried many of the suggested answers online like calling layoutIfNeeded, but that didn't work anywhere I tried it.

While accepted answer can help in some cases I would like to share some other ways to fix this issue:
Check Content Hugging Priority of your cell's components (especially if you're using stack views inside cell)
In your cell add:
override func didMoveToSuperview() {
super.didMoveToSuperview()
layoutIfNeeded()

I found a working solution: In my GameCell class, I overrided layout subviews:
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.layoutIfNeeded()
positionUIElements()
self.awayTeamAbbrev.preferredMaxLayoutWidth = self.awayTeamAbbrev.frame.size.width
self.homeTeamAbbrev.preferredMaxLayoutWidth = self.homeTeamAbbrev.frame.size.width
}

44 is defailt height for UITableViewCell in UITableView, try to override func
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension;
}
or return exact value, in your case 166.75

I had the same problem using UITableViewDiffableDataSource. The tableView set the correct cell height after I attempt to scroll manually the content.
The tableView was inside on an UIViewController. The dataSource was applied on the viewDidLoad method.
I reloaded the tableView content using tableView.reloadData() inside the viewWillAppear and the problem was solve.

Related

Custom TableViewCells do not show up

So I am pretty new to iOS development. I try to create everything programmatically so my Storyboard is empty. I'm currently trying to get a TableView with custom cells. The TableView is running and looking fine when I use the standard UITableViewCell. I created a very simple class called "GameCell". Basically, I want to create a cell here with multiple labels and maybe some extra UIObjects in the future (imageView etc.). For some reason, the custom cells do not show up.
Game cell class:
class GameCell: UITableViewCell {
var mainTextLabel = UILabel()
var sideTextLabel = UILabel()
func setLabel() {
self.mainTextLabel.text = "FirstLabel"
self.sideTextLabel.text = "SecondLabel"
}
}
Here the additional necessary code to get the number of rows and return the cells to the TableView which I have in my ViewController. self.lastGamesCount is just an Int here and definitely not zero when I print it.
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.lastGamesCount
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as! GameCell
In my viewDidLoad() I register the cells like this:
tableView.register(GameCell.self, forCellReuseIdentifier: cellID)
When I run everything the Build is successful I can see the navigation bar of my App and all but the TableView is empty. I go back to the normal UITableViewCell and the cells are showing up again. What am I missing here? Any help is appreciated.
Thanks!
The problem is you need to set constraints for these labels
var mainTextLabel = UILabel()
var sideTextLabel = UILabel()
after you add them to the cell
class GameCell: UITableViewCell {
let mainTextLabel = UILabel()
let sideTextLabel = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setLabel()
}
func setLabel() {
self.mainTextLabel.translatesAutoresizingMaskIntoConstraints = false
self.sideTextLabel.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(mainTextLabel)
self.contentView.addSubview(sideTextLabel)
NSLayoutConstraint.activate([
mainTextLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor),
mainTextLabel.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor),
mainTextLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor,constant:20),
sideTextLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor),
sideTextLabel.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor),
sideTextLabel.topAnchor.constraint(equalTo: self.mainTextLabel.bottomAnchor,constant:20),
sideTextLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor,constant:-20)
])
self.mainTextLabel.text = "FirstLabel"
self.sideTextLabel.text = "SecondLabel"
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

Check if cell completely displayed in collectionview

I have a collection view where the cell is of the size exactly to the collectionView, so each cell should occupy the whole screen. I have implemented a functionality where the cell is snapped to the complete view whenever it's dragged or decelerated through the scroll. This is how the UX works.
https://drive.google.com/open?id=1v8-WxCQUzfu8V_k9zM1UCWsf_-Zz4dpr
What I want:
As you can see from the clip, the cell snaps to the whole screen. Now, I want to execute a method after it snaps. Not before or not when it's partially displayed.
Following is the code I have written for snapping effect :
func scrollToMostVisibleCell(){
let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint)!
collectionView.scrollToItem(at: visibleIndexPath as IndexPath, at: .top, animated: true)
print("cell is ---> ", visibleIndexPath.row)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollToMostVisibleCell()
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
scrollToMostVisibleCell()
if !decelerate {
scrollToMostVisibleCell()
}
}
If I use willDisplayCell method, then it' just going to return me as soon as the cell is in the view, even if it's just peeping in the collectionView.
Is there a way where I can check if the cell is completely in the view and then I can perform a function?
I have scrapped the internet over this question, but ain't able to find a satisfactory answer.
Here is a complete example of a "full screen" vertical scrolling collection view controller, with paging enabled (5 solid color cells). When the cell has "snapped into place" it will trigger scrollViewDidEndDecelerating where you can get the index of the current cell and perform whatever actions you like.
Add a new UICollectionViewController to your storyboard, and assign its class to VerticalPagingCollectionViewController. No need to change any of the default settings for the controller in storyboard - it's all handled in the code below:
//
// VerticalPagingCollectionViewController.swift
//
// Created by Don Mag on 10/31/18.
//
import UIKit
private let reuseIdentifier = "Cell"
class VerticalPagingCollectionViewController: UICollectionViewController {
private var collectionViewFlowLayout: UICollectionViewFlowLayout {
return collectionViewLayout as! UICollectionViewFlowLayout
}
private var colors: [UIColor] = [.red, .green, .blue, .yellow, .orange]
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
self.collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// enable paging
self.collectionView?.isPagingEnabled = true
// set section insets and item spacing to Zero
collectionViewFlowLayout.sectionInset = UIEdgeInsets.zero
collectionViewFlowLayout.minimumLineSpacing = 0
collectionViewFlowLayout.minimumInteritemSpacing = 0
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let cv = collectionViewLayout.collectionView {
collectionViewFlowLayout.itemSize = cv.frame.size
}
}
override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if let iPath = collectionView?.indexPathsForVisibleItems.first {
print("DidEndDecelerating - visible cell is: ", iPath)
// do what you want here...
}
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return colors.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
cell.backgroundColor = colors[indexPath.item]
return cell
}
}
Using followedCollectionView.indexPathsForVisibleItems() to get visible cells visibleIndexPaths and check your indexPath is contained in visibleIndexPaths or not, before doing anything with cells.
Ref : #anhtu Check whether cell at indexPath is visible on screen UICollectionView
Also from Apple : var visibleCells: [UICollectionViewCell] { get } . Returns an array of visible cells currently displayed by the collection view.

UITableView inside UIView

I'm trying to create a dynamic UITableView in a xib, so what I think might work is to put the table view inside a blank UIView. Then I would subclass this UIView and make it adhere to the protocols UITableViewDelegate and UITableViewDataSource. Can someone guide me with this, because I've tried many things, but none of them worked. Many Thanks!
EDIT:
I'll show you what I tried before (sorry, don't have the original code):
class TableControllerView: UIView, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView()
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
tableView.frame = self.frame
tableView.delegate = self
tableView.dataSource = self
}
func tableView(...cellForRowAt...) {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.titleLabel?.text = "test title"
return cell
}
}
but in this case the table view ended up being too big and not aligned with the view, even though I set it's frame to be the same as the view
EDIT 2 :
My code now looks like this:
class PharmacyTableView: UIView, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var pharmacyTableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//PROVISOIRE depends on user [medications].count
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = "text label test"
cell.detailTextLabel?.text = "detail label test"
return cell
}}
table view is initialized but only shows up when height anchor is constrained, which I don't want because it might grow or shrink depending on user data. I guess I'll be done after solving this?
P.S. : Also, thank you very much to the people that took the time to help me :)
EDIT 3:
So, I've changed the class of the table view to this:
class IntrinsicResizingTableView: UITableView {
override var contentSize:CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height)
}
}
And now everything works fine! Finally!
Your question is unclear. Based on your final statement the following code is what you should use to keep the table view and its superview aligned.
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
If this is not the answer you're looking for, please clarify your problem. Also it would help if you explain what you're doing this for? Why do you need this superview?

Using auto layout in UITableviewCell

I am trying to use auto sizing UITableView cells in swift with snapKit! relevant flags set on the UITableView are as follows:
self.rowHeight = UITableViewAutomaticDimension
self.estimatedRowHeight = 70.0
I have a UITextField defined in my customUITableviewCell class like:
var uidTextField: UITextField = UITextField()
and the initial setup of the text field in my custom UITableViewCell looks like this:
self.contentView.addSubview(uidTextField)
uidTextField.attributedPlaceholder = NSAttributedString(string: "Woo Hoo", attributes: [NSForegroundColorAttributeName:UIColor.lightGrayColor()])
uidTextField.textAlignment = NSTextAlignment.Left
uidTextField.font = UIFont.systemFontOfSize(19)
uidTextField.returnKeyType = UIReturnKeyType.Done
uidTextField.autocorrectionType = UITextAutocorrectionType.No
uidTextField.delegate = self
uidTextField.addTarget(self, action: "uidFieldChanged", forControlEvents: UIControlEvents.EditingChanged)
uidTextField.snp_makeConstraints { make in
make.left.equalTo(self.contentView).offset(10)
make.right.equalTo(self.contentView)
make.top.equalTo(self.contentView).offset(10)
make.bottom.equalTo(self.contentView).offset(10)
}
when I run the code it shows up cut off and gives me an error in the console that reads:
Warning once only: Detected a case where constraints ambiguously
suggest a height of zero for a tableview cell's content view. We're
considering the collapse unintentional and using standard height
instead.
Is there something wrong with my autoLayout constraints or is this an issue with UIControls and autosizing of UITableView cells?
In SnapKit (and Masonry) you have to use negative values to add a padding to the right or bottom of a view. You are using offset(10) on your bottom constraint which causes the effect that the bottom 10pt of your text field will get cut off.
To fix this you have to give your bottom constraint a negative offset:
uidTextField.snp_makeConstraints { make in
make.left.equalTo(self.contentView).offset(10)
make.right.equalTo(self.contentView)
make.top.equalTo(self.contentView).offset(10)
make.bottom.equalTo(self.contentView).offset(-10)
}
Or you could get the same constraints by doing this:
uidTextField.snp_makeConstraints { make in
make.edges.equalTo(contentView).inset(UIEdgeInsetsMake(10, 10, 10, 0))
}
When you are using the inset() way you have to use positive values for right and bottom inset.
I don't understand why SnapKit uses negative values for bottom and right. I think that's counterintuitive and a bit confusing.
EDIT: This is a little example that is working fine (I hardcoded a tableView with 3 custom cells that include a UITextField):
ViewController:
import UIKit
import SnapKit
class ViewController: UIViewController {
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.dataSource = self
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 70
tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "CustomCell")
tableView.snp_makeConstraints { (make) -> Void in
make.edges.equalTo(view)
}
}
}
extension ViewController: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath)
return cell
}
}
CustomTableViewCell:
import UIKit
class CustomTableViewCell: UITableViewCell {
let textField = UITextField()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
textField.attributedPlaceholder = NSAttributedString(string: "Woo Hoo", attributes: [NSForegroundColorAttributeName:UIColor.lightGrayColor()])
textField.textAlignment = NSTextAlignment.Left
textField.font = UIFont.systemFontOfSize(19)
textField.returnKeyType = UIReturnKeyType.Done
textField.autocorrectionType = UITextAutocorrectionType.No
contentView.addSubview(textField)
textField.snp_makeConstraints { (make) -> Void in
make.edges.equalTo(contentView).inset(UIEdgeInsetsMake(10, 10, 10, 0))
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

Single line text takes two lines in UILabel

As you can see in the picture the middle cell has a UILabel that consumes two lines, but the text is actually a single line. It seems that if the text needs only few characters to create a new line, iOS assumes that it already has 2 lines. This is odd.
This is how I create the label:
self.titleLabel.lineBreakMode = .ByTruncatingTail
self.titleLabel.numberOfLines = 0
self.titleLabel.textAlignment = .Left
The constraints are set once:
self.titleLabel.autoPinEdgeToSuperviewEdge(.Top)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Leading)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Trailing)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Bottom)
The weird thing is, when the table is scrolled so that the odd cell disappears and scrolled back again it has its normal height. After scroll:
Any ideas whats wrong? I am using swift, xcode6.1 and iOS8.1
TableViewController:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(CityTableViewCell.self, forCellReuseIdentifier:"cell")
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 52
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell: CityTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as? CityTableViewCell {
// Configure the cell for this indexPath
cell.updateFonts()
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
if indexPath.row == 1 {
cell.titleLabel.text = "Welcome to city 17, Mr. Gordon F."
} else {
cell.titleLabel.text = "Lamar!!!"
}
// Make sure the constraints have been added to this cell, since it may have just been created from scratch
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
return cell
}
return UITableViewCell();
}
I think you met this bug: http://openradar.appspot.com/17799811. The label does not set the preferredMaxLayoutWidth correctly.
The workaround I chose was to subclass UITableViewCell with the following class:
class VFTableViewCell : UITableViewCell {
#IBOutlet weak var testoLbl: UILabel!
//MARK: codice temporaneo per bug http://openradar.appspot.com/17799811
func maxWidth() -> CGFloat {
var appMax = CGRectGetWidth(UIApplication.sharedApplication().keyWindow.frame)
appMax -= 12 + 12 // borders, this is up to you (and should not be hardcoded here)
return appMax
}
override func awakeFromNib() {
super.awakeFromNib()
// MARK: Required for self-sizing cells.
self.testoLbl.preferredMaxLayoutWidth = maxWidth()
}
override func layoutSubviews() {
super.layoutSubviews()
// MARK: Required for self-sizing cells
self.testoLbl.preferredMaxLayoutWidth = maxWidth()
}
}
OP-Note:
It seems that auto layout does not calculate the width of the UILabel layout correctly. Setting the preferred width to the parents width in my UITableViewCell subclass solves my problem:
self.titleLabel.preferredMaxLayoutWidth = self.frame.width
Found on SO: https://stackoverflow.com/a/19777242/401025

Resources