My project currently has a view, which is a UIViewController, in that view controller I have a UIViewTable that shows a list of devices that I can tap on to open a settings menu for each device. The list shows up correctly and the tapping actions are also working fine.
My problem is that the display cuts off the cells a little to the left and right. The separators also had a margin to the left but I could fix it in my storyboard by changing the separator inset to custom and = 0.
The overwhelming response to that problem is to add :
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
Under
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
But this had no effect for me. Here's the picture of my display, any help would be really appreciated!
Note: If that helps, when I tap the cell, the highlight does cover the whole width which seems a little odd to me, but I'm not sure what to do with that information.
As the whitespace only appears to the right and the left of your cels, it's clearly part of the cels themselves. Check the Background color of the cell view - it's probably set to white.
Related
If you make a tableVIew cell editable so that you can rearrange the order of the cells these hold-and-drag marks appear in the cells:
Great, but the downside is that they push the whole cell to the left, so the auto layout that is supposed to put things in the middle doesn't work the way I intend. Labels that I want to be in the middle are now a bit to the left. Even if I constrain things to the cell itself I get the same result since it appears that the whole cell is pushed.
Is there a way to get the width of the edit-marks (not sure about the technical term for it) so that I can take that into account when setting up the auto layout? Or is there another way to get around this?
Not widely published as far as I know. You could try setting the layout margins within the cell to offset the displacement from the reorder view?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.layoutMargins = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 10)
return cell
}
40 seems to be the magic number but you could amend with a bit of trial and error.
I have the below controller with the cell's, content view's, and table view's backgroundColor set to .clear, however, there is still a white background which I can't figure out what it is corresponding to.
It is due to Your table view cell colour.
Select table view cell:
Set its background colour as clear.
I suggest you to debug it in your Xcode. To do this you can launch it on you device(or on simulator), click 'Debug View Hierarchy' button.
Then by Mouse Click + Dragging on the screen rectangle you can rotate all layers of your screen and see which view is causing that white background.
this white background is due to table view cell select table view cell and from navigator make its background clear. or you can do it in
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath) as! YourCellClass
cell.backgroundColor = .clear
}
this will solve your problem...
:)
The block of code below, which alters the lblBody's frame size seems to be having zero effect on my UITableViewCells.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ChatCell", forIndexPath: indexPath) as! ChatCellTableView
//cell.lblBody.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.lblBody.text = msgSections[indexPath.section].msg[indexPath.row].text
cell.lblUsername.text = msgSections[indexPath.section].msg[indexPath.row].username
cell.lblTime.text = msgSections[indexPath.section].msg[indexPath.row].time
cell.lblBody.frame.size.height = 900
cell.lblBody.textColor = UIColor.greenColor()
return cell
}
When I build and run my app, my UITableViewCell's labels are the exact same size as they are in the storyboard editor. I already removed all constraints with the "clear constrains" command, but for some reason my uilabels still set their size based off of the storyboard.
I set the text color to an ugly green just as a sanity check. My uilabel's colors are green, their text is proper, everything but the label's size seems to be effected.
I need this because the array which returns the text to be shown in the UILable also contains the precise width and height the labels will need to work properly.
UPDATE:
Another curious thing I've found is that in RAM the heights of my lblBody's are correct when I print the following:
print("THE CURRENT HEIGHT: ")
print(cell.lblBody.frame.size.height)
return cell
When I build and run the app and scroll through the cells, I get the following output:
THE CURRENT HEIGHT:
900.0
THE CURRENT HEIGHT:
900.0
THE CURRENT HEIGHT:
900.0
THE CURRENT HEIGHT:
900.0
So it looks like the height is being properly set within the UILabel's properties, but I don't see UILabels with the height of 900. All of my UILabel's sizes are the same as they appear in the story board editor. Reloading/refreshing the tableview's data does not fix the issue here.
The actual drawing of your cell occurs after cellForRowAtIndexPath occurs. To have control after bounds have been set, I would suggest you take a look at modifying the label within the override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) method.
I just want a simple UITableView with the ability to slide left to delete. Everything works okay except the right constraint on the textview in my prototype cell seems to get shifted after swiping to delete. Here's my code for the table:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Uses prototype cell from Interface Builder called "CommentTableCell"
let tableCell = tableView.dequeueReusableCellWithIdentifier("CommentTableCell", forIndexPath: indexPath) as! CommentTableCell
tableCell.userInteractionEnabled = true
tableCell.selectionStyle = .None
//Sets the text for the cells in the comment table
tableCell.commentText.text = comments[indexPath.row]
tableCell.timeLabel.text = commentTimes[indexPath.row]
return tableCell
}
//As many rows in the table as there are comments
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return comments.count
}
//Allows the user to delete comments
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
comments.removeAtIndex(indexPath.row)
commentsTable.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.layer.borderWidth = 0.75
self.view.layer.borderColor = borderColor.CGColor
self.view.layer.cornerRadius = 5.0
//Gets rid of the line between comment cells
commentsTable.separatorStyle = UITableViewCellSeparatorStyle.None
commentsTable.backgroundView = nil
//Sets the height of the row to fit text boxes
self.commentsTable.estimatedRowHeight = self.commentsTable.rowHeight
self.commentsTable.rowHeight = UITableViewAutomaticDimension
}
}
This is what it looks like after I've swiped left to edit and then swiped back right on the top cell (stackoverflow won't let me use pictures yet). Note the right sides of the gray text boxes and labels in each cell are no longer aligned. The gray text box in the cells has a right constraint of -8 so I'm also confused why there's any margin on the other cell's text boxes at all.
Thanks for any help you can give me, I'm still fairly new to Swift! I've tried to find anything like this question on stack overflow and I've come up empty.
Okay so I found a way to fix this and thought I'd post here in case anyone else runs into the same problem.
It still seems like a bug in XCode to me as I can't think of any time you might want the behavior described above. Basically, if the text box constraints in the prototype cell are set to "constrain to margins" in the Pin auto layout menu then the right horizontal constraint will be reset (as far as I can tell) randomly after you slide to delete and then slide back.
Just uncheck constrain to margins when you add those constraints and it should fix this problem!
I want to have a button in my custom cell which is right aligned. Doing it with auto layout and setting a horizontal space from the right and centering it vertical does not show the Button in my cell because it is too far at the right.
In the picture you can see my cell. But it is not shown like this in the actual app.
Edit: I tried your answer but this does not seem to work. My TableView looks kind of strange in the Emulator:
Is it possible that there is a problem in my controller?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("eventCell", forIndexPath: indexPath) as EventTableViewCell
let event = items[indexPath.row]
cell.contentView.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
return cell
}
Do the following for that Button:
Set Width and Height constraints
set Vertical center constraints to the cell view
Set horizontal/trailing(may be 10px) space to the right side of the cell view
Sry I am dumb.
Everything was fine with my Cell layout. I was so focused on the cell that I forgot to add constraints to the TableView. Adding these constraints fixed the problem.