Why is this happening? I'm not using IB by the way.
CODE:
cell = self.tableView.dequeueReusableCellWithIdentifier("NormalCell") as UITableViewCell
cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.textLabel?.numberOfLines = 0
cell.textLabel?.text = items[indexPath.row]["text"] as? String
IMAGES:
You need to set the constraints properly. Check out my screenshots below!
1) Set Constraints
http://prntscr.com/6r5i2z
2) Check Table Properties
http://prntscr.com/6r5i8y
3) Run the project! That's it!
Let me know, if you have any other issues!
Related
Hi im trying to add a checkbox beside my task text but it wont follow my constraints, constraints set as pic one, but when I compile it shows me like in picture two.
I tried to put them inside a stackView but it made no difference.
The problem was I was setting the tableview.textfield.text = to the task text.
What I wanted to do was to set my customTableViewCells textfield.text = to the task.
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
and the put the cell.textFeild.text = myTask
I have been having alignment issues.The message label keeps over writing the time stamp and the username is disappearing. I have tried the suggested constraints but nothing has worked for me.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
//Transform Data From ^ to load at the bottom
tableView.transform = CGAffineTransform (scaleX: 1,y: -1);
cell?.contentView.transform = CGAffineTransform (scaleX: 1,y: -1);
cell?.accessoryView?.transform = CGAffineTransform (scaleX: 1,y: -1);
//Set username label to display username
let usernameLabel = cell?.viewWithTag(1) as! UILabel
usernameLabel.text = generalRoomDataArr[indexPath.row].username
//Set message label to display message
let messageLabel = cell?.viewWithTag(2) as! UILabel
messageLabel.text = generalRoomDataArr[indexPath.row].message
messageLabel.numberOfLines = 0
//initialize UI Profile Image
let imageView = cell?.viewWithTag(3) as! UIImageView
//Make Porfile Image Cirlce
imageView.layer.cornerRadius = imageView.frame.size.width/2
imageView.clipsToBounds = true
//Set timeStampLabel to current time AGO
let timeStampLabel = cell?.viewWithTag(4) as! UILabel
timeStampLabel.text = generalRoomDataArr[indexPath.row].timeStamp
timeStampLabel.numberOfLines = 0
//Loading and change of Usesrs profile image on chat cell
let userProfileChatImage = generalRoomDataArr[indexPath.row].photoURL
//Load profile image(on cell) with URL & Alamofire Library
let downloadURL = NSURL(string: userProfileChatImage!)
imageView.af_setImage(withURL: downloadURL as! URL)
// your cell coding
return cell!
}
Problem
StoryBoard
//TableView Cell word wrap (Dynamic Text)
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.estimatedRowHeight = 78
self.tableView.rowHeight = UITableViewAutomaticDimension
Do i need to set the time stamp label to 0 as well?
messageLabel.numberOfLines = 0
I used to use labels like you are using and found out about NSMutableAttributedString(). There are tons of videos and web pages that explain how it works. If there are people who like a specific one, they can link it in the comments! I use the attributedString with a UITextView in order to display everything (text-based) that I need. I defer to this video (beginning at 12:00 mins) on YouTube because it is difficult to explain in text. In order to tackle the problem of creating a UITableViewCell that fits varying amounts of text, you will want to drag in a UITextView that does not have a specific width or height constraint. This will allow it to change with the cell. Then, using the code you have...
self.tableView.estimatedRowHeight = 78
self.tableView.rowHeight = UITableViewAutomaticDimension
Your cells will dynamically change. One trick to get this working is to do the following steps:
1) Disable user interaction on the UITextView using isUserInteractionEnabled = false
2) Uncheck Editable and Selectable in the Interface Builder (Storyboard), as well as unchecking Scrolling Enabled in the IB.
If you've already captured all the constraints in that screenshot. I think you missed a vertical spacing constraint between timestamp label and message label. After add this constraint, you will also need to change vertical compress resistance and hugging priority for 1 of three labels. I recommend decrease message label's.
Is it possible to set a value for a UILabel in a custom UITableViewCell without reloading the cell?
I've tried many permutations of:
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell1
cell.customLabel.text = "text goes here"
.. and I don't understand why this won't work. I've got a UITextField in the UITableViewCell, and when I set the textField value I'd like only the label to update, but not reload the entire cell. Any suggestions?
cell.customLabel.setNeedsDisplay()
If you want to update the UIViews, you need to put them in main thread:
dispatch_async(dispatch_get_main_queue(),{
cell.customLabel.text = "text goes here"
})
So I have a TableView with 2 prototype cells. Here is my code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ItemDetailsCell
cell.centerImage.image = mainImage
cell.heading.text = detailsHeadings[indexPath.row]
let headingString = detailsHeadings[indexPath.row]
cell.body.text = details[headingString]
tableView.rowHeight = cell.labelBlack.frame.height + 40
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! ItemDetailsCell
cell.heading.text = detailsHeadings[indexPath.row]
let headingString = detailsHeadings[indexPath.row]
cell.body.text = details[headingString]
let bodyString = details[headingString]
let labelWidth = Int(cell.body.frame.width)
println(labelWidth)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: labelWidth, height: 10000))
label.text = bodyString
label.numberOfLines = 100
label.font = UIFont(name: "OpenSans-Light", size: 12.0)
label.sizeToFit()
tableView.rowHeight = label.frame.height + 3
return cell
}
}
So the second prototype cell has just two labels with the values being assigned from a Dictionary. The cell size needs to expand or contract based upon how many lines of text there are. In auto layout I have the number of lines set to 0 so it will pick however many lines are needed. This works fine except when you scroll within the app it will snap the view up as users scroll back up from the bottom. Is there a way to avoid this?
Thanks in advance for any help.
I found this actually after spending some more time looking:
http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
It gave me what I needed. I removed the parts of my code that set the rowHeight and then used the viewDidLoad method as well as auto layout to constrain my cell sizes and after a bit of trial and error it is working without snapping to place when you scroll.
You are changing the rowHeight of the tableview. That's most likely very wrong. The rowHeight of the tableView is the default for all rows that don't have their own height, so you are effectively changing the height of all cells.
You should use the delegate method tableView:heightForRowAtIndexPath:
I'm trying to implement a table by code, so far so good but when I scroll down and up in my table my rows went crazy, I did a little research and I think its because of the way I reuse my cells, but the examples I found were all in Obj- C, So could you please help me to understand the problem? Here are my function where I implement the cells:
override func tableView(tableView: (UITableView!), cellForRowAtIndexPath indexPath: (NSIndexPath!)) -> UITableViewCell{
let sectionA = seccionesDiccionario[indexPath.section]
let sectionName = tableDataSwiftDictionary[sectionA]!
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("CellId") as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CellId")
cell!.backgroundColor = UIColor.clearColor()
cell!.textLabel?.textColor = UIColor.darkTextColor()
let selectedView:UIView = UIView(frame:CGRect(x: 0, y: 0, width: cell!.frame.size.width, height: cell!.frame.size.height))
selectedView.backgroundColor = UIColor.orangeColor().colorWithAlphaComponent(0.3)
cell!.selectedBackgroundView = selectedView
cell!.textLabel?.text = sectionName[indexPath.row]
cell!.textLabel?.numberOfLines = 0
cell!.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell!.textLabel?.sizeToFit()
}
return cell!
}
Thank you so much.
when I scroll down and up in my table my rows went crazy
what do you mean by 'went crazy'.
One thing that I can see in the above code: You should move the text assignment out of the if statement. You want the 'textLabel' to show the String in the 'sectionName' array at the given indexPath.row. Currently, you are creating some cells and then - when you start scrolling your tableView - the cells are reused but the textLabel's text is not set, so it will always show its initial value.
Move this line
cell!.textLabel?.text = sectionName[indexPath.row]
out of the if{} block. Maybe that's all you need to do here.
EDIT
btw: since you're calling sizeToFit on the textLabel, I assume you want the cell to be high enough to display all the text. Note that sizeToFit will not be enough in order to achieve that. You'll have to either use Auto-sizing cells using AutoLayout (> iOS8) or implement the tableView:heightForRowAtIndexPath: delegate method and return the calculated cell height there.