UITableViewCell

I have a table view in a view controller. I have set up the constraint of and location of this table using the Anchor Constraint commands. I have added a button to my table view and I have created a class UITableViewCell for the cell. I want to be able to locate and position this button based on the table view constraint but I had no luck so far. Can anyone help me figure this out?

Replies

Do you want to position the button inside the cell ?


Could you edit a drawing here (no image, hand drawing) to explain ?

Do you want at XX or yy ?

What is it exactly what you don't know how to do ?


___________________________________

| view of viewController |

| |

| ______________________________ |

| | tableView | |

| | ____________________________ | |

| | _______________________yy___ | |

| | _______________________yy___ | |

| | _______________________yy___ | |

| |

| XX |

|_________________________________|

Hi I want to place it inside the cell in yy. I have already used ontentEdgeInsets = UIEdgeInsets(top: 0, left: 2.5*wdth/3, bottom: 0, right: 0) but it is not a general way of defining it. Because I want the button to be all the way to right of the row. I have already made the constraints of my tableviewusing trailing and leading anchor with respect to the view it is at.

I actually managed to figure it out:

I used this in the tableviewcell class:

myBtn.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true


PS: I put the tableview in the story board and then adjust the constraint of it programmatically. I also added a button and defined a custom cell class and declare the button in that class.

ANother convenient way, if you want to do everything in IB, is to define a subClass of UITableViewCell for this cell.


And create a xib in IB for it.

Then, you just create constraints for the button inside the cell frame. Very easy.


In that case, don't forget to register the cell in the table view.


Good luck, and don't forget to close the thread.

Did you solve your issue ?