How to integrate a "like" button in swift tableviewCell - ios

What is the logic i should follow to integrate a like button in a tableview cell?
How do you update a text label inside the cell signaling how many likes it has in real time IE: when you click the like button it either adds a like or removes a like?
The button also is highlighted when current_like = true
and not highlighted when current_like = false
Where do I update that kind of stuff?
How can you update a cells label and display the new label from within the cell? or is it NECESSARY to reload cell for row at Index Path?

The two main problems/steps you have to achieve are:
- Update the label with the new like
- Update the datasource of the table to keep the data persistent.
So, what I would make:
Set your custom UITableViewCell as target for the Button, so the cell can know when the button was clicked. In the target function/selector you should update the label.
Now, you have to inform the datasource of your table that the cell has a new like. You can create a protocol in UITableViewCell and set the TableDataSource as its delegate. Then, when the button was clicked you can notify the delegate.
You can achieve the same behavior with NSNotificationCenter, instead delegation.
Regards ;)

To change the cell's content without reloading you need to create a pointer to that cell. You can change your cell's parameters directly using a pointer without reloading cell. So it would be something like
self.myCell.label.text = something
And to assign the pointer to your cell you must put something like this in your cell adding method:
self.myCell = yourLikeCounterCell

Related

Implementing different user interaction function for subviews of a cell

I have a UICollectionView which allows for a user to select a cell and upon doing so view 'A' will appear. I am wondering if it is possible for the subviews of this cell, ex: UIlabel and UIImageView to provide a different functionality for when they alone are selected. For example, if the UIImageView is selected, I want to segue to view 'B' as opposed to 'A'.
I have attempted to implement a UITapGestureRecognizer for both the label and the image, however, the cell's functionality overrules and the resulting view is still 'A'. Any ideas?
Thank you in advance.
What you want to achieve is possible through delegates if you don't have custom cell make a custom cell class then inside custom cell declare your protocol
I assume you have to disable the default behavior of the collection view cells:
cell.selectionStyle = UITableViewCellSelectionStyle.none
However, if you set that and you encounter an overlap issue, please take a look at the 'cancelTouchesInView' property of the 'UITapGestureRecognizer'. Basically, by setting that to false, you allow the children to also receive touch actions.
Furthermore, do not forget each gesture recognizer should have it's own method for you to be able to segue into two different places.

How to add a subview to a TableViewCell?

I need to add a subview to TableViewCell when the button in TableViewCell is clicked it should show another tableview as a subview and height of the tableview should be dynamic according to number of cell.
How can I do this?
Above screenshot is taken from a very popular shopping app and I need to do the same in my project.
You can easily achieve this using simple UITableView. For that you need to set all your main categories as your UITableview section and respected sub-categories can be added to respective rowOfSection.
You can use just one table view with UITableViewStyleGrouped style, and set "Men","Women","Kids & Baby" as TableView sections header, keep an boolean value to determine the result of "numberOfRowsInSection:" of each section and reload tableview.
See the below link at github:
https://github.com/OliverLetterer/SLExpandableTableView
This contains the Expandable TableView, which you required. You have to implement SLExpandableTableViewDelegate and SLExpandableTableViewDatasource which contains different method, in which you have to provide inner tableview as well.
Hope this helps you.
You need to do some work on your own I can give the directions that'll give you a way from my point of view :-
Make a Custom cell which you want to expand.
While designing the cell make its height in storyboard like 200 or so according to your need and add all the elements those you want to see when the cell is expanded.
You'll need two delegate methods first -didSelectItemAtIndexPath and second HeightForRowAtIndexPath at index path.
First You need to make sure the user taps on the button or cell that you want to expand , and to achieve that you need to call didSelectItemAtIndexPath.
Once you get your cell Position, in HeightForRowAtIndexPath check the indexpath is equal to your cell's indexpath,if yes then return the exact height(ie:200) of your cell otherwise return the default(ie:70) height of you cell.
Note : In didSelectItemAtIndexPath you need to call to method to update the current cell
[self.tableView beginUpdates];
[self.tableView endUpdates];

Updating label in a cell of UITableView

My UITableViewCell has a cell template which is created from another nib file. The cell has a UIlabel object. Now, once the UITableView has loaded and the text has been displayed, and if I want to change its value by clicking a button from another cell, How should I do it ?
I have updated the text of the UIlabel but how to show it on the screen? Should I reload the entire table? Kindly let me know if there is any good way to do it.
You can use KVO for this purpose. Each cell observes the model, and when it changes, update some fields.

Change another cell in dynamic prototype UITableView when a switch in one cell change

I used to use static UITableView but the table is too long and overflow the memory.
I switched a dynamic prototype UITableView with 1 type of cell, which has an UISwitch in it.
One of the cell, when turning on the switch, will turn off the switch of another cell. These cells have fixed index.
The IBAction method is in my UITableViewCell subclass and I don't want to add the UITableView as a property in my UITableViewCell.
How do I achieve the above effect?
I'm planing to use an id or similar to distinguish between the cells as each cell's switch has different effects, that doesn't solve the above requirement.
Thanks,
I would add a block property to your cell which you can use to notify your controller of changes in the switch. See my answer below to a question on this:
How can I get index path of cell on switch change event in section based table view
All your logic can now be implemented in the view controller.
You are best to create a data model in the view controller which the cells simply provide views and controls onto. When you flick one switch and the block fires, update the data model and simply reload the table. Any cells affected will show the new data model positions for their switches. Avoid using one cell to adjust another. Just update the model and reload the cells.

change the position of textfield inside an expandable uitableview cell in iOS

Hi, I am new to iOS and i am trying to have two text fields inside a custom table view cell and the cells are dynamic, so I will be having two text fields in each cell and when a cell is selected the cell will expand
How can I reposition the text field when the cell expands? One text field should be on the top and the other one on the bottom inside the tableview cell.
How can i access the uitext field delegate inside uitable view delegate
i.e.:
textFieldShouldBeginEditing inside tableView:didDeselectRowAtIndexPath:indexPath
Thanks in advance
I would suggest having two different custom UITableViewCells - one for viewing and one for editing. When didSelectRowAtIndexPath is called, replace the selected cell with the second custom cell designed for editing. When the user is finished, replace it again with the original custom cell type.
For your second question about accessing subviews of the UITableViewCell, you have several options. I'm going to assume you're using storyboards.
If you created a custom class for your UITableViewCells, you can add the UITextView as a class property and connect it as a outlet from your storyboard to your class by making it an IBOutlet. That way you can access it via self.nameOfTextView.
If you don't want to create a custom class for your UITableViewCells, you can assign the subviews tags and access them via (UITextView *)[cell.contentView viewWithTag:1]. This second option can also be used if you aren't using storyboards.

Resources