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()
Related
how to add a right detail to a cell that already have left detail and subtitle. and how to give a textLabel to the right detail?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let monster = monsters[indexPath.row]
cell.textLabel?.text = monster.name
cell.detailTextLabel?.text = monster.subtitle
return cell
}
Try this
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let monster = monsters[indexPath.row]
cell.textLabel?.text = monster.name
cell.detailTextLabel?.text = monster.subtitle
let label = UILabel.init(frame: CGRect(x:0,y:0,width:100,height:20))
label.text = monster.name
cell.accessoryView = label
return cell
}
Click on your tableView cell. It's easier to do it in document outline.
Change the style attribute in Attribute Inspector from Basic to Custom. You will now be able to add a label. The basic template is set, so you can't alter it. I believe this was the issue you were having.
I'm sure after you place the label you know what to do, but just in case:
Place the new label at the right margin of the cell and pin it to the top, bottom, and right, using Autolayout.
Reconnect your title, subtitle IBOutlets, and create a right label IBOutlet, connect it, and you should be done.
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'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
}
}
I have a expandable tableView, in which when i expand a section, than there are three cell. On firth Cell there is only name and in second cell. It have a big content. Now I want to auto adjust this label height and width according to content.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tblView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell
let dataArrayTblView = dataArrayForTableView
let titleName = dataArrayTblView.valueForKey("name")
let dueDate = dataArrayTblView.valueForKey("deadlinedate")
let description = dataArrayTblView.valueForKey("description")
cell.titleLabel.text = titleName[indexPath.row] as AnyObject! as! String!
cell.dueDateLabel.text = dueDate[indexPath.row] as? String
cell.descriptionLabel.text = description[indexPath.row] as? String
cell.descriptionLabel.sizeToFit()
cell.textLabel?.backgroundColor = UIColor.clearColor()
cell.selectionStyle = .None
return cell
}
But not getting complete content
Try to set this. It will automatically adjust the height of the row for you. If it is not working, then you have something wrong with your constraints inside your storyboard.
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
You should use UITableViewAutomaticDimension as row height something like,
// this should be set in viewDidload
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
for that you must use autolayout and should set proper constraints to your label in cell.
Constraint should be in linear chain, I mean your label's top and bottom both constraint must be set and your label should resize according to content so your cell will resize accordingly!
You can refer Self-sizing Table View Cells by Raywenderlich !
Put below in viewDidLoad and set autolayout as per below screenshots.
tblview.rowHeight = UITableViewAutomaticDimension
tblview.estimatedRowHeight = 44
Screenshot 1
Screenshot 2
Screenshot 3
Screenshot 4
Use heightForRowAtIndexPath method to adjust height of row. Calculate
size of string with boundingRectWithSize this method. example:
Try This:
if let YOUR_STRING:NSString = str as NSString? {
let sizeOfString = ns_str.boundingRectWithSize(
CGSizeMake(self.titleLabel.frame.size.width, CGFloat.infinity),options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: lbl.font], context: nil).size
}
Can anyone tell me how to change the text size in a tableView so that the text in the screenshot below fits within the width of the screen:
The current tableView code is:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel!.text = self.dataArray[indexPath.row]
cell.imageView!.image = UIImage(named: "20x20_empty-round-radiobutton-choice-ui")
return cell
}
Thanks in advance!
Try the following code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel!.text = self.dataArray[indexPath.row]
cell.textLabel.font = UIFont(name: cell.textLabel.font.fontName, size:15) // Change the font size as per your requirement
cell.imageView!.image = UIImage(named: "20x20_empty-round-radiobutton-choice-ui")
return cell
}
Set autoshrink minimum fontScale. If your text is long enough to exceed the actual Label Frame,then your font will be shrinked upto minimum font scale you provide in storyboard or xib.