I set my tableview constraints pinch to left and right at zero in the storyboard.
But depends on my indexPath.row, I need to set new constraints (inset & width). I've tried this without success.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch dataType! {
case .mycase:
let cell = tableView.dequeueReusableCell(withIdentifier: "NoBrandDeviceFoundTableViewCell", for: indexPath) as! NoBrandDeviceFoundTableViewCell
tableView.isScrollEnabled = false
var frame = tableView.frame
frame.size.width = 343
cell.frame = frame
tableView.contentInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
return cell
}
}
The frame of the cell won't be changed it's been set equal to the width of the table , you can hook the leading/left & trailing/right constraints of the items you want to shift and change their constant's value according to that indexPath in cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NoBrandDeviceFoundTableViewCell", for: indexPath) as! NoBrandDeviceFoundTableViewCell
if indexPath.row == <#someValue#> {
cell.lblLeadingCon.constant = <#someValue#>
}
else {
cell.lblLeadingCon.constant = 0
}
return cell
}
you need to take cell leading constanint - nslcLeadingCell
you need to take cell trailing constatint - nslcTrailningCell
based on indexpathrow contion match you need make condition
if indexpath.row == 5
cell.nslcLeadingCell.constant = 10
cell.nslcTrailningCell.constanct = 15
Related
I'm having some issue with the table vie cells, only after some scrolling the cells get their right height.
I tried some answers from this post Text. Added layoutIfNeeded() but i doesn't seem to work
video showing the issue
My Code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView === MainTable{
if let cell = tableView.dequeueReusableCell(withIdentifier: "MedName", for: indexPath) as? MedCell{
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.white.withAlphaComponent(0.0)
cell.selectedBackgroundView = backgroundView
cell.setMedName(name: self.medCatalog[indexPath.row].nombre, uso: self.medCatalog[indexPath.row].uso )
cell.layoutIfNeeded()
cell.updateConstraintsIfNeeded()
cell.layoutSubviews()
self.cellBool[indexPath.row] = true
self.collapsedCellHeight[indexPath.row] = cell.medText.bounds.height
return cell
}
}
self.cellBool[indexPath.row] = false
self.collapsedCellHeight[indexPath.row] = 0.0
self.expandedCellHeight[indexPath.row] = 0.0
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.cellBool[indexPath.row] != nil {
if self.cellBool[indexPath.row]!{
let nameHeight = self.collapsedCellHeight[indexPath.row]!
let topAnchor: CGFloat = 12.0
let bottomAnchor: CGFloat = 10.0
let cellHeight = nameHeight + topAnchor + bottomAnchor + 9 + 2
return cellHeight
}
}
return 85
}
EDIT
I forgot to mention that the main goal to make an expandable and collapsible row, that why there's a UIlabel below the blue UIlabel.
The blue uilabel text is gotten from a json. Some values from the json are a bit long so the uilabel create another line to show all the content. That's why i need to provide the cell height, because some labels my have 1 line and other more than one line and i need to calculate the blue label's height and on didSelectRowAtIndexPath expand the entire cell. I know how to expand and collapse de row what i don't know is why the table behave like so
I fixed it by replacing
cell.layoutIfNeeded()
cell.updateConstraintsIfNeeded()
cell.layoutSubviews()
with
cell.layoutIfNeeded()
cell.layoutSubviews()
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
I'm trying to build two (or more) custom cells into table View and without custom classes for each one, I'm using storyboard to cell prototyping and tableView.dequeueReusableCell(withIdentifier: "customCellName", for: indexPath) to create different cell from cellForRowAt method.
I need cell type 1 with different height than cell type 2 but if a try to use func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat the cell type 2 is not displayed correctly.
I try to build a "dummy project" to understand the right way, here my code:
var cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let value = dataArray[indexPath.row]
if (value == "second") || (value == "Hawaii") {
cell = self.tableView.dequeueReusableCell(withIdentifier: "cellTwo", for: indexPath)
}
let lbl = cell.viewWithTag(100) as! UILabel
lbl.text = dataArray[indexPath.row]
return cell
In ViewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.estimatedRowHeight = 200
}
This is the result:
Every cell with the same size, is not working for me.
If you want to create a custom cell appearance, you will have to create a subclass of UITableViewCell - there are some great tutorials on how to do that.
You then have to register your custom cell, so that your UITableView knows about it. See register(_:forCellReuseIdentifier:) method.
In your tableView(_:heightForRowAt:) you can do write the following code in order to figure out the height of particular cell:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
let value = dataArray[indexPath.row]
if (value == "second") || (value == "Hawaii") {
return 180.0
} else {
return 40.0
}
}
In Core Data I have a one-to-many relationship between contact and location entities. In my tableview I want to display the contact information as header in a Section and the associated locations for each contact inside the cell as multiple rows.
I am able to display the header (with contact information) and the cell ( with locations) dynamically. I set the cell as Custom with Row Height 200. I also set the rowHeight in my program as below:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//tableView.estimatedRowHeight = 200.0
//tableView.rowHeight = UITableViewAutomaticDimension
//return 200.0
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Here is my cellForRowAt function:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellView:UIView = UIView()
let cell:EndPtTableViewCell = tableView.dequeueReusableCell(withIdentifier: "endPtCell", for: indexPath) as! EndPtTableViewCell
// Configure the cell...
let endpt:EndPtListTableViewController.EndPt = allEndpts[indexPath.row]
var yVal:Double = 10.0
for locationDict in locationDictArray {
let location:String = locationDict["locationName"]!
let zip:String = locationDict["zip"]!
let locationAndZipLabel = UILabel()
locationAndZipLabel.text = location + ", " + zip
locationAndZipLabel.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)
locationAndZipLabel.frame = CGRect(x: 30.0, y:yVal, width: 350, height: 15)
locationAndZipLabel.tag = 5
yVal = yVal + 25.0
cellView.addSubview(locationAndZipLabel)
}
cell.contentView.addSubview(cellView)
return cell
}
The cell displays the location and Zip values for each location in different rows, but does not expand the cell row height dynamically. As you can see the second row is partially displayed.
How can I dynamically expand the cell Row Height based on number of rows inserted?
I'm struggling to load a cell in cellForRowAtIndexPath in such way so that I can give a custom offset to one cell only - in my case only to Cell 1, Section 1. I'd like to give a custom offset to the cell, as the same cell (xib) is used in other parts of the project where it extends to the edges of the table without any offset. All my cells are designed as *.xib files. I'm willing to set something in awakeFromNib() or other methods, if there's the need for that.
Is there any way to set the cell offset, inset, margins, padding (not sure about the correct wording) at time of the creation or at time of loading of the cell?
I've tried to set margins in this way, but it doesn't work:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.section == 0) && (indexPath.row) == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCell
cell.layoutMargins = UIEdgeInsetsMake(10, 10, 10, 10)
return cell
}
} else {
// Load other cells
}
I think you're close with your cellForRowAtIndexPath implementation. Try applying the margins to the cell's contentView (the superview for your cell's content, and a subview of the cell). You may also have to set the cell's background colour to clear (the contentView will still be white).
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.section == 0) && (indexPath.row) == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCell
cell.contentView.layoutMargins = UIEdgeInsetsMake(10, 10, 10, 10)
return cell
}
} else {
// Load other cells
}
I have a UITableViewCell, drawn in Storyboard.
However, running it on simulator, it seems that there is white line in each cell divider as well header-cell divider.
A is Header in UITableViewCell
A11 - A13 is content in UITableViewCell
How to remove it? It seems I can't find any answer out there.
Update:
Solution here only moves the divider color to left with no margin and keeps the header divider persists. Is there any way to remove the white color for both header and cell without moving the divider line?
You could simply choose to set Separator for the tableview to "None". Then subclass the UITableviewCell and create the border by adding a CALayer as a sublayer to the cell at awakeFromNib.
Using Swift 4.X and adopting the fastest hacking-method, you can improve code using extensions:
extension UITableViewCell {
var isSeparatorHidden: Bool {
get {
return self.separatorInset.right != 0
}
set {
if newValue {
self.separatorInset = UIEdgeInsetsMake(0, self.bounds.size.width, 0, 0)
} else {
self.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
}
}
}
}
Then, when you configure cell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath)
switch indexPath.row {
case 3:
cell.isSeparatorHidden = true
default:
cell.isSeparatorHidden = false
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
if cell.isSeparatorHidden {
// do stuff
}
}