Removing undesired left indent on UITableViewCell - swift

I have a Table View within my main UIViewController that has one non-editable prototype cell. I am trying to remove the left indent from the table view cell. No matter what method I use, the indent does not go away.
Here is what I have tried:
On the Table View: In Interface Builder > Attributes Inspector, I set Separator Inset to Custom with left inset of 0.
On the Table View Cell: In Interface Builder > Attributes Inspector, I set indentation Level and Width to 0. I also changed Separator to Custom with left 0.
I also tried the following in code:
myTable.layoutMargins = UIEdgeInsets.zero
myTable.separatorInset = UIEdgeInsets.zero
And this inside of cellForRowAt:indexPath
cell.indentationLevel = 0
cell.indentationWidth = 0
cell.layoutMargins = UIEdgeInsets.zero
return cell
Here's a screenshot of what's occurring:
I need all of the "Hello's" to left-align with the red section header text.
How can I remove the left-indent of the content?
Thanks.

Have you tried using a custom cell with a label and at the required distance from the left edge?

I'm sure that layoutMargins is the reason of indent on UITableViewCell. But I think you can't change layoutMargins. I almost don't use textLabel of UITableViewCell. You can refer to Setting layoutMargins of UIView doesn't work.

Try this -
helloLabel.textAlignment = .Left
cell.addSubview(helloLabel)

Related

Automatic height for TextView using autolayout in cell for tableView

I want to set the height of the TextView as per content in a cell for tableview.
i pinned the top of text view to the job title UILabel and bottom to the content view (parent) but it does not working. is there any tutorial for this kind of layout positioning ?
How i add constraint to fix the automatic height issue?
Current constraints
As I can see you have some other setup as well in the cell. to support dynamic height
you can give fix height constraint to UITextView.
take outlet of that constraint.
update it before you return the cell from cellForRowAt method.
so, your cellForRowAt method will end up like
tableCell.textViewHeightConstraint = textView.contentSize.height
tableCell.setNeedsUpdateConstraints()
tableCell.setNeedsLayout()
return tableCell
Solved this way.
Change the TextView to UILabel, Then set Lines property in the attribute inspector to 0.
on viewDidLoad
tableview.rowHeight = UITableView.automaticDimension
tableview.estimatedRowHeight = 229.0
It works.
More https://www.raywenderlich.com/8549-self-sizing-table-view-cells
You can apply Self-Sizing Table View Cells by adding two lines:
tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableViewAutomaticDimension
You should read this document from Apple:
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html

Swift - Custom cell width and height for textlabel and detailTextLabel

I wanna manage the long text in textlabel and detailtextlabel for the cells in UITableview because if the textlabel has a long text the detailedtextlabel truncate with "..." and viceversa.
Here's the problem:
So I wanna know if there's a way to manage the space between textlabel and detailtextlabel so there's no truncate and give them more lines. I tried with:
...
if tableData[indexPath.row].clave.count > 6 {
cell?.detailTextLabel?.numberOfLines = 5
cell?.textLabel?.numberOfLines = 5
} else {
cell?.detailTextLabel?.numberOfLines = 1
}
cell?.textLabel?.text = nueva_cadena_string
cell?.detailTextLabel?.text = tableData[indexPath.row].clave
cell?.textLabel?.textColor = UIColor.brown
cell?.textLabel?.font = UIFont.boldSystemFont(ofSize: 15.0)
cell?.textLabel?.lineBreakMode = .byWordWrapping
return cell!;
But it doesn't work very well, it seems to give them just 2 lines, not 5 and I can't change the width for the textlabel and detailtextlabel.
Try making your cell custom as this:
Try setting the tableviews row dimension to automatic
yourTableView.rowHeight = UITableViewAutomaticDimension
yourTableView.estimatedRowHeight = 80.0
try setting both textlabels number of lines either in the storyboard or in code to 0 as then they will all have the amount of lines they need.
You can subclass UITableViewCell and override the layoutSubviews() method. You will need to change the layout of textLabel and detailTextLabel there. Remember that these elements lie inside contentView, not in the cell itself.
But better you add two custom labels on the Storyboard and configure them as you want. It's easier.

Swift: Remove contentInset for UITextView in TableViewCells

I am attempting to set the contentInsets to zero for my textView that I place inside my tableViewCells so that I can align my text with the label and imageView bubble. My current implementation looks like this.
I adopted the solution in this post but does not seem to work for my case.
Blank space at top of UITextView in iOS 10
The solution in the above post is for uiviews but mine is in a tableViewCell, which automaticallyAdjustsScrollViewInsets is not available in tableViewCell.
Any help here pls?
Setting contentInset to zero will not work since it's applied to UIScrollView.
The spacing that you want to remove is actually controlled by NSTextContainer inside of UITextView.
To remove the spacing just put this in your cell (for example inside awakeFromNib):
override func awakeFromNib() {
super.awakeFromNib()
// Removes UITextView inner spacing
textView.textContainerInset = .zero
textView.textContainer.lineFragmentPadding = 0.0
}

Setting left inset of empty uitableviewcells (placeholder cells) to 0 to eliminate left padding?

I was able to find an answer in stack overflow that led me to successfully eliminate the default left padding occurring on my cell separator by setting left inset = 0 in the storyboard of Xcode. However the empty cells beneath my populated custom cells still have padding on their left separators.
How can I fix this?
In your storyboard for the Table, Set the separator inset
And finally in awakeFromNib for custom table cell
Swift 3.x
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.layoutMargins = UIEdgeInsets.zero
self.preservesSuperviewLayoutMargins = false
}
For Swift 2.x
self.layoutMargins = UIEdgeInsetsZero
self.preservesSuperviewLayoutMargins = false
Output

TableView Cell Separator Line Not Extending Across the Entire Cell

I am working on a new project and have used a storyboard for the UI. All of my tableViews have an issue with the line separator. The picture below shows two lines. The first is a blue one which was set in the attributes inspector. The second one is black and was added with an imageView that I placed in the cell. The line does extend to the right side of the cell but not the left. Any ideas?
first set table view
1.set separatorInset of tableview instance to UIEdgeInsetsMake(0, 0, 0, 0)
second set cell
1.set preservesSuperviewLayoutMargins of cell (instance) to NO
2.set layoutMargins of cell to UIEdgeInsetsZero
3.saet separatorInset of cell to UIEdgeInsetsZero
Hope to help you
You have to make sure your cell property called preservesSuperviewLayoutMargins is set to false (default)
cell.preservesSuperviewLayoutMargins = false
Then you just have to set your cell layoutMargins to zero
cell.layoutMargins = .zero
You need also to select you cell separator, select custom and change left value from 15 to 0