How do i assign TextFields value (i.e text) to UITableViewCell? - ios

What i want to do is that if i write something in TextField then it must get displayed in TableView

It is straight forward.
Implement delegates/datasource methods for UITableView.
Make an array to hold user input(s). This array will be used as datasource to populate the table.
Reload the tableView.

I always use searchBar in tableView, Implement searchBar's delegate ,then set cell's text changed when you input searchString.

Related

Swift UITextField inside TableViewCell

I am using a table view and have a UITextField inside it, but this text field don't detect the click on the cell. How I can fix this?
subclass the uitextfield, in prepareforreuse, set the delegate to nil, in the cellforrowatindexpath in the uitableview/datasource delegate, set the the textfield delegate to self, then implement the tableview methods there. to determine which textfield is clicked, index at point functions for tableview in the UITableView header, once you find that point, convert it to NSIndexPath. use that indexpath to find the array value where you store the textfield text values, update that value, then reload the row or the entire tableview and viola, you have your textfield population and working correctly,
lastly, show your code, this is very difficult to answer without showing your code

How to integrate a "like" button in swift tableviewCell

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

How to manage selected cell

I'm building a blog reader app with a list of post objects in an NSArray.
cell.postTitle.text=post.Title;
I'm trying to find out which post has been selected and then set the corresponding cell.postTitle.text to a lighter font.
Somehow I have to remember this state when the navigationcontroller segue back to the top level (and destroyed?).
Can anyone tell me the best way to do this? Each post object has a postID. Maybe I can store this in NSUserDefault, but the list of read postID might grow too big over time.
Also there is didSelectRowAtIndexPath but in this method, I can't access the cell properties directly right?
If you are using a UITableView to display your information you should use the delgate didSelectRowAtIndexPath.
In order to make this work with a custom interface (I assume you have), you should create your own custom cell, subclass UITableViewCell and create a controller and xib. You can then register your xib to the table and use that. Then you can call the needed attributes.
Somehow I have to remember this state when the navigationcontroller segue back to the top level (and destroyed?).
This is best handled by using a delegate method by creating a custom protocol:
https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html
When your user clicks the cell, you would call a delegate method which could set the postID value back in the top controller.
Also there is didSelectRowAtIndexPath but in this method, I can't access the cell properties directly right?
Yes, you can get at the cell. Check out this question and answer:
How to reach the current selected cell in didSelectRowAtIndexPath?
Alternatively, if you want to get at the data you used to create the table cells in the first place, in your didSelectRowAtIndexPath method you can used the indexPath.row value to get the value from your array that is at that index position and pass that via a delegate method.
Found a solution.
Just add the postId of the selected post to an NSMutableArray in didSelectRowAtIndexPath. Then in cellForRowAtIndexPath, I check to make sure that the postID doesn't exist in my self.readPostID array.
If it does I set the font to Helvetica Light, else set it to Helvetica Bold.
First I had to make sure to [self.tableview reloadData] every time viewdidappear.
Second, I just save my NSMutableArray of readPostId to NSUserDefault everytime viewDidDisappear and reload the array in viewDidLoad.
Also I had to make sure that cell reuse doesn't affect the font. The else condition fixed that.

Updating UITableView cells according to the text entered in a text filed that is in the same main view

Is it possible to update the cells of a table according to the value entered in the text field which is in the same view or when clicking on a button which is in the same view. can this be done when all the components are in the same view? of course i know that i will have to use a cell and a table view an all in that view.
If it's possible please hint me to what method in the TableView Delegate I should work with to make this happen or if I have to write a custom method.
Thanks in advance.
You should look into UISearchController
Links to explore:
http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view
http://jduff.github.io/2010/03/01/building-a-searchview-with-uisearchbar-and-uitableview/
http://www.iphonedeveloperdiary.com/2010/07/adding-a-uisearchbar-to-top-part-tableview/
http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/38913-slicks-uitableview-series.html
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UISearchDisplayController_Class/Reference/Reference.html
OR...
If you want to do it using something like a simple UITextField, then in the -textFieldShouldReturn, you can use NSPredicate to create an array of searched objects and then simply can do a [self.tableView reloadData].
(just ensure that the datasource of your tableView is using this array)
You can do it just by using
[tableView reloadData];
On Clicking if button please ensure to update UITableView Datasource Array to update.

How do I get information from a UITableViewCell?

I have a TableView with a prototype cell, in this prototype cell I have a text field.
Well, what I need is to get a information from this text field when it changes. I need to do that to feed a object.
How can I do that ?
Short answer - you don't.
Slightly more in depth answer...
The UITableViewCell is a "view". It is used only to display information to the screen. It is not for storing data in.
In a UITableViewController (i.e. UITableViewDatasource and UITableViewDelegate) you should have an NSArray (or an NSFetchedResultsController) that you use to store information in.
Then using the delegate methods you populate the table with that data.
If the data changes then you reload the table to update the cells.
What should never happen is the following...
Load the table by passing in data to the cell.
The cell then changes its own data and changes what is on the screen.
The controller then reads that data out of the cell.
No, no, no, don't do this.
What should happen is this...
Load the table and configure the cell display to represent the correct part of the data.
When the button is pressed (or text field is changed, etc...) in the cell then call a method back in the controller to update the data accordingly.
Now the data has changed, reload the cell.
It will now show the correct information based on the new data.
You need a custom cell with a public property (IBOutlet) UITextfield. Then you can set your ViewController as the textField's delegate in cellForRowAtIndexPath,
cell.textField.delegate = self;
in this case your ViewController has to implement UITextFieldDelegate protocol.

Resources