I have table view.In this table view i have a custom cell.In cell i have a label which will have a circle around it at runtime.Now i have written some code so make it circle but it does not show circle when table view is loaded first.When i scroll the tableview then it shows circle around the UILabel.
I am using below code.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellId="NotificationCell"
let cell:NotificationCell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! NotificationCell
cell.label_taskCount.layer.cornerRadius = cell.label_taskCount.frame.size.height/2
cell.label_taskCount.layer.borderWidth = 1
cell.label_taskCount.layer.borderColor = UIColor.white.cgColor
cell.label_taskCount.layoutIfNeeded()
cell.label_taskCount.text = String(indexPath.row)
return cell
}
Try setting corner radius in layoutSubViews in NotificationCell,
override func layoutSubviews() {
super.layoutSubviews()
self.label_taskCount.layer.cornerRadius = self.label_taskCount.frame.size.height/2
self.label_taskCount.layer.borderWidth = 1
self.label_taskCount.layer.borderColor = UIColor.white.cgColor
}
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()
My requirement is to highlight the contentView of the selected cell. As of now the issue is previously selected cell (contentView) is also get highlighted. Code as bellow
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.viewWithTag(20)?.layer.borderWidth = 2.0
cell?.viewWithTag(20)?.layer.borderColor = UIColor.blue.cgColor
}
Read up cell reuse. You need to reset your cell's layer in prepareForReuse.
Follow these 3 steps:
Go to storyboard and select your cell.
In the right panel in Attributes Inspector, change Selection Style to Default.
Also select your tableview and in project Attributes, change Selection to Single Selection
The issue is cell reuse; you need to reset to original state of the cell like so:
class HelightTableViewCell: UITableViewCell {
override func prepareForReuse() {
super.prepareForReuse()
self.layer.borderWidth = 0.0
self.layer.borderColor = UIColor.clear.cgColor
}
}
While creating the cell on prepareForReuse change to original state and on did select set the properties like so:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) as? HelightTableViewCell {
return
}
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.blue.cgColor
}
I want to change all color of UIView to pink not blue when first load tableview not just when scrolled, how to fix ? what happen now, color just changed when you scroll to bottom the tableview and back to top scroll then change the top blue to pink again
I'm confused for a week for this stuff, haha
My TableViewCell
override func awakeFromNib() {
super.awakeFromNib()
containerItem.addViewRounded(cornerRadius: 8)
containerItem.addShadow()
}
override func layoutSubviews() {
containerHeader.roundCorners([.topLeft, .topRight], radius: 8)
}
func configureCell(exc: Exceptional) {
containerHeader.backgroundColor = UIColor(named: "Cinderella")
lblStatusApprove.textColor = UIColor(named: "Carnation")
}
this is my tableview related code :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let exc = arrayOfExceptional[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "ExceptionalTableViewCell", for: indexPath) as! ExceptionalTableViewCell
cell.lblApprover.text = exc.approver
cell.lblCreatedAt.text = exc.createdAt
cell.lblSubmitDate.text = exc.submitDate
cell.lblTypeExceptional.text = exc.typeName
cell.lblExcStartEnd.text = "\(exc.timeStart) - \(exc.timeEnd)"
cell.lblDescription.text = exc.excDescription
cell.lblSubmitDate.sizeToFit()
cell.lblExcStartEnd.sizeToFit()
cell.lblApprover.sizeToFit()
cell.lblCreatedAt.sizeToFit()
cell.configureCell(exc: exc)
return cell
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
loadMoreControl.didScroll()
}
My Viewdidload :
excTableView.register(UINib(nibName: "ExceptionalTableViewCell", bundle: nil), forCellReuseIdentifier: "ExceptionalTableViewCell")
content and reload table here
Hey guys i found the solution to fix my problem, because the cell is not created yet fully in the first load so we need to add "willDisplay cell"
so i add this code:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let cell = cell as? ExceptionalTableViewCell {
cell.containerHeader.backgroundColor = UIColor(named: "Cinderella")
cell.lblStatusApprove.textColor = UIColor(named: "Carnation")
}
}
I have a view one top of UITableView. If I drag top view out, UITableViewCell text colour is changing to white, even if I am chasing to gray.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! CustomTableViewCell
cell.productNameLabel.text = "chi"
cell.productNameLabel.textColor = UIColor.darkGray
return cell
}
If I don't put top view to UITableView then it is showing grayColor. I tried to log cell in console when text becomes white.
UITableViewCell;
frame = (0 0; 375 60);
clipsToBounds = YES;
hidden = YES;
autoresize = W;
layer = <CALayer: 0x604000427da0>>
It shows hidden is Yes. I have not set anywhere hidden. Is it because of dequeue issue?
try with different string and different color,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! CustomTableViewCell
cell.productNameLabel.text = "testing label"
cell.productNameLabel.textColor = UIColor.redColor
return cell
}
In Xcode 8 with Swift 3 I'm using custom cell with auto layout. Information into cell inserted from JSON, so my code looks like:
//the method returning each cell of the list
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCell(withIdentifier: "Feedcell", for: indexPath) as! FruitTableViewCell
//getting the hero for the specified position
let hero: List_Feed
hero = heroes[indexPath.row]
cell.labelAnswer.text = hero.answer
cell.labelQuestion.lineBreakMode = .byWordWrapping
cell.labelQuestion.text = hero.question
cell.labelQuestion.sizeToFit()
cell.labelAnswer.sizeToFit()
return cell
}
Problem is that I'm getting equal labelQuestion heigh for each cell, but text size and line size are different. How to solve this? :c
As Cell is reusing the Label You have to empty label before use for next cell
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCell(withIdentifier: "Feedcell", for: indexPath) as! FruitTableViewCell
//EMPTY LABELS
cell.labelAnswer.text = ""
cell.labelQuestion.text = ""
self.layoutIfNeeded()
//getting the hero for the specified position
let hero: List_Feed
hero = heroes[indexPath.row]
cell.labelAnswer.text = hero.answer
cell.labelQuestion.lineBreakMode = .byWordWrapping
cell.labelQuestion.text = hero.question
cell.labelQuestion.sizeToFit()
cell.labelAnswer.sizeToFit()
return cell
}
Hope it is helpful to you
1- set vertical content hugging priority off today label to 1000
2- hook the bottom of the label [ that is under today label ] to the top of the imageView Below it
3- hook bottom of the most-down label to bottom of contentView
Edit :
Add this 2 lines before return cell in cellForRowAt
cell.layoutSubviews()
cell.layoutIfNeeded()