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
}
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()
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()
This is happening but I don't know why. It is a standard tableviewcell with a label and an image on it
Here is the code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = patientsTableView.dequeueReusableCell(withIdentifier: "patientTableViewCell", for: indexPath)
let text = "Bob Marley"
cell.textLabel?.text = text
return cell
}
When I do not put an image view in the object inspector but like below, it solves the problem. I am not sure why though
cell.imageView?.image = UIImage(named: "patients")
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
}
I have a regular UITableView with single selection enabled. My problem is that if the user selects multiple rows then the original rows remain selected. I also have a problem where the highlight remains gray no matter if I set the cell.selectionStyle = UITableViewCellSelectionStyle.Blue
My view controller is defined in the Storyboard.
Table View
Content: Dynamic Prototypes
Selection: Single Selection
Show Selection on Touch [X]
Background: Black Color
Index Row Limit: 0
Table Cell View
Style: Custom
Selection: Blue
Background: Black Color
Here are some screenshots:
Here is my code:
class AreaViewController: UITableViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
self.tableView.backgroundColor = backgroundColour
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("areacell", forIndexPath: indexPath) as! UITableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.Blue
cell.textLabel?.text = "Cell Contents"
cell.textLabel?.textColor = UIColor.whiteColor()
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let cell = tableView.dequeueReusableCellWithIdentifier("areacell", forIndexPath: indexPath) as! UITableViewCell
}
}
I must be missing something obvious but I've not been able to see anything non standard.
From the UITableViewCell Class Reference
UITableViewCellSelectionStyleBlue The cell has a default background
color when selected.
In iOS 7, the selection color is no longer blue. Use
UITableViewCellSelectionStyleDefault instead.
If you want a special background color for selected cells you have to set the cells' backgroundView:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.redColor()
cell.selectedBackgroundView = backgroundView
return cell
}
Looks like this:
Argh! I found it at last. Seems like I was calling let cell = tableView.dequeueReusableCellWithIdentifier("areacell", forIndexPath: indexPath) as! UITableViewCell in the didSelectRowAtIndexPath method. Removing it caused everything to start working again. Obvious really. Thanks for your help zisoft in putting me on the right road.