Change cell content of UITableView in swipe action - ios

I have a label in cell and I want to change the position of the label when swiping the cell.
For example, when swiping cell to left, label will be moved to right.
I didn't find any delegate for swiping to add some point in label constraint.

There is UISwipeGestureRecognizer so you can use that for recognition. You can use that for cell, in you cellForRow add that to each cell. Than you can use delegate for the cell to return self. Add leading or trailing constraint of your label as outlet in your cell. You can change constraint like this:
labelConstraint.constant = 0
UIView.animateWithDuration(1.0) {
self.view.layoutIfNeeded()
}
Edit:
If swipe to delete is enabled: Override willTransitionToState method in your custom cell and detect delete state or use tableView's willBeginEditingRowAtIndexPath and change there constant of your constraint

Related

How do I expand and collapse a UICollectionViewCell with swift?

The Collection view cell would need to show and hide additional tools for the device being controlled. The button that collapses would be at the bottom of the UICollectionViewCell. Whenever the button is pressed it would animate downwards revealing the fan or lights extra features or animate upwards when hiding the extra features. I only need help with expanding and collapsing by using the button at the bottom of the cell. An image is attached showing an example.
Add a height constraint for your high/med/low selector.
Connect that constraint to var in your cell class like this:
#IBOutlet weak var checker1HeightConstraint: NSLayoutConstraint!
set that constraint to a value you want (either 0, for hidden, or 50 or 60 if not hidden)
have a method where you set the value:
func toggleControls(visible: Bool)) {
checker1HeightConstraint = visible? 50 : 0
setNeedsLayout()
}
If you are not sure how to set the outlet, just ctrl-drag from the constraint in you storyboard or xib to your custom cellView class. It will give you options to create it.

How to change layout of a UITableViewCell with a UIStackView

I have a custom UITableViewCell with a vertical UIStackView. The stack view in-turn has 2 UILabels, one of it is hidden by default. How to change the height of the cell to show both labels in the stack view properly when the second label is unhidden. On a button click when i show the second label, it tries to show both label with the same height overlapping each other, however when i scroll the view up and down, it re-renders it ok.
I tried to call SizeToFit and LayoutIfNeeded with no help. How do i change the height of the row/cell to show both labels in the stackview to look proper?
You can call
tableView.beginUpdates()
tableVIew.endUpdates()
After tap on button, and your cell change height. It solution is compatible with animation.
On the button click, the label 2nd is showed, you should reload the table view, or reload exactly the cell, the height of cell will change and the cell will be re-rendered
Try reloading that row with reloadRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation) after you click the button that shows both labels.

UILabel shifts to the right when cell is tapped

I have a UITableView and have cells in it and I am using textLabel and detailTextLabel in it along with a accessoryView which is a disclosureIndicator. These are my constraints.
if (cell.contentView.constraints.count == 0) {
cell.textLabel?.translatesAutoresizingMaskIntoConstraints = false
cell.detailTextLabel?.translatesAutoresizingMaskIntoConstraints = false
let margins = cell.contentView.layoutMarginsGuide
// TextLabel constraints
cell.textLabel?.leftAnchor.constraint(equalTo: margins.leftAnchor).isActive = true
cell.textLabel?.centerYAnchor.constraint(equalTo: margins.centerYAnchor).isActive = true
// DetailTextLabel constraints
cell.detailTextLabel?.rightAnchor.constraint(equalTo: margins.rightAnchor, constant: -10).isActive = true
cell.detailTextLabel?.leftAnchor.constraint(equalTo: (cell.textLabel?.layoutMarginsGuide.rightAnchor)!, constant: 10).isActive = true
cell.detailTextLabel?.centerYAnchor.constraint(equalTo: margins.centerYAnchor).isActive = true
}
Now when the Table View is first loaded I see everything fine i.e detailTextLabel is -10 points away from contentView but when I tap the cell the detailTextLabel shifts to the right and hugs to the contentView as if -10 point constraint isn't even there. Now when I come back to this VC again, I still see detailTextLabel hugging to contentView i.e reloadData is not taking any effect. But if I dismiss this VC and come back again here my constraint and in place like they should be and the problem comes up again when I tap on the cell to go to the next screen.
I have a reloadData() call in my viewWillAppear which gets called but doesn't produce any effect.
I hope I am able to explain myself, am I doing anything wrong here?
The problem is that you are using a built in cell type, where the text label and detail label are built in and don’t belong to you. They belong to the cell. The cell repositions them on selection. You are fighting the cell.
If you want to customize a cell’s appearance and change the position of the cell labels, use a custom cell type, and give it custom labels that belong to you. You can given them constraints right in the storyboard designer. Do not use the built in text label and detail label at all.
You cannot change the constraints of the cell labels if it is not a custom type cell. If you want custom constraints then you should create your own custom cell which inherits from UITableViewCell and set your own constraints.

How to add button end of uitableview with swift

I want to add button or row end of uitableview. I used tableview footer but its not solution for me. I also tried others way but ı didnt found any result. How can ı pass this problem.
Thanks..
Use UIViewController instead of UITableViewController:
In storyboard add UITableView to your view controller
Create an IBOutlet for UITableView - connect it
Conform your view controller to UITableViewDelegate and UITableViewDataSource
set tableView.delegate = self and tableView.dataSource = self
In storyboard add a button bellow table view and set top button's constraint to be few (e.g. 4) points away from table view's bottom constraint
Create IBOutlet for button and connect it
Add height constraint for your button - set it to 0.0
Set button's title to empty string ("")
Create IBOutlet for button's height constraint and connect it
Check when your table view reaches the end - to check whether your table reached the bottom you can implement tableView:willDisplayCell:forRowAtIndexPath: and check if indexPath.row is the last item in your data source (there are other ways too so you are free to check them)
When your table reached the end - simply set button's title to your desired title with button.setTitle(for) and animate button's constraint to another height (big enough to see the whole button)
When button is pressed you can just do the opposite animation: set button's title to "" and animate height constraint change back to 0.0

Overlapping UITableviewCells

In a tableview one cell overlaps the cell displayed under it when initially presented. When you scroll down until the top cell is not longer visible and then scroll back up the cell are ok again.
I deactivated the separators with this line:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
The second cell has a view that fill it from top to bottom, that was a border radius. When the cells are initially presented the border is covert.
How can i fix the spacing of the tableviewCells when they are initially loaded? Can i set the z index the second cell, so that the second cell is above the first one?
I finally found that the constraint of the the top cells where causing the trouble. I found a workaround by storing the layout constraint in a property and adjusting the constant of the constraint as needed.
For "Can i set the z index the second cell, so that the second cell is above the first one?"
UITableViewCell is subclass of UIView so in table view delegate use this :
secondCell.layer.zPosition = 1;

Resources