Get data from UITableView cell - ios

I have a problem with getting data from cells in UITableView.
I have a lots of cells (other class with xib) with some people info in table view. Every cell has own switch.
Under table is button that will send message to every person with switch pressed on.
I set property in people class that will remember if switch for person is on. Every switch in cellForRow method has set selector with method for switch action.
Problem is that I have no idea, where to set person property value about switch state. In selector method I don't have access to person info. I have sender, but no info about person.

I'd do this in one of 2 ways depending on what exactly you're trying to do:
Pass a pointer of the person object to the cell when you're configuring it and let the cell handle updating the property that holds the value of the switch.
Make a protocol with a function
-(void)switchInCell:(UITableViewCell*) cell valueModifiedTo:(NSInteget) newValue
Adopt the new protocol in your view controller, add a delegate property of type new protocol to the cell, call that delegate method from within the cell if the switch value changes.
In that method implementation in your view controller, you can do
NSIndexPath* indexPath = [theTable indexPathForCell:cell];
PersonClass* person = Your_DataSource_Array [indexPath.row];
//modify person properties

You can set the UIView tag property to a number, for each of the subviews of the table View Cell's "content" property, at the cell's creation time (e.g. when you add all the UIViews, e.g. labels, buttons, etc...), giving each subview a unique value.
Later, if just given the UITableViewCell, you can iterate through the subviews of is content view and locate your switch (and any other UIViews) by checking their tag value.

If you are using custom cells, you can have method for your switch inside the cell's implementation. From the method, you can get back to the data source using delegation, or notifications and you can pass indexPath of the cell for further identification of the object in the data. You can have a property to store the indexPath of the cell in implementation of the cell's class and you can set the value to that property in cellForItemAtIndexPath: method. Good Luck!

you said you have sender. then you need to just make custom subclass of the control you are using and create a custom property like this.
#property (nonatomic, strong)NSIndexPath *indexPath;
and set it in cellforrowatindexpath. when your selecter calls the you can parse the sender to the custom created class and get the value form the property. try this once.

Related

Referencing a particular table view cell from table view controller

I have a UITableView with cells that display an audio waveform image and a playback button. Tapping the button causes the audio to be played back, of course. In order to reduce memory usage, I have a single instance of AVAudioPlayer declared in my table view controller. I defined a protocol that has playAudio(url:URL) and stopPlayingAudio() methods and my table view controller conforms to this protocol. Anytime a new cell is dequeued, I assign the table view controller as the delegate for the cell so that when the user taps on the playback button in the cell UI, it calls the playAudio(url) delegate method. This seems to be working well enough but I've run into a problem now.
I'm calculating a percent complete value as the audio is playing and I'd like to update the table view cell UI with this value but I'm not sure how to reference the correct cell from the table view controller. It seems like the cell that was tapped on to start the audio playback could end up getting recycled if it scrolls off the screen (unless I'm misunderstanding how cells get dynamically dequeued). Is there a way to do this?
If you know what row of the table you're looking for, you can ask the table view for the corresponding cell:
guard let cell = tableView.cellForRow(at: indexPath) as? WaveformCell {
cell.fractionComplete = ...
}
There are a few ways in which you can achieve what you intend to achieve,
If you are maintaining a datasource to create a cell from(which you should if you are not), maintain the state of the cell, this can include the percentage played of the cell's url and the state whether the item isPlaying, which will be false by default.
Once the states are in place, you need to now update this state, so you will have to add create a protocol(say AudioStateObserverProtocol) for sending this data to the cell, this protocol may have a method which periodically updates the cell UI as the player plays (something like, updatePlayDuration: or something of this sort), this will make sure that you get the value of how much of the asset has played. So when the user taps the play button instead of calling playAudio(url:URL) you can update the protocol method to playAudio(url:URL, stateObserver: TheTableViewCell), which the table view controller will set to as the delegate of type AudioStateObserverProtocol.
protocol AudioStateObserverProtocol {
func updatePlayDuration(to time: CMTime)
}
Add another protocol method stopObserving(cell: TheTableViewCell) to the protocol you have defined with playAudio(url:URL) and stopPlayingAudio()
The next step is how to make sure that the cell on reuse does not still receive/use the update, to do this you can make sure that when you setup the cell in your cellForRow datasource methods you first call the stopObserving(cell: TheTableViewCell). In your implementation of this method inside the tableview controller check for the instance of the cell against the param of type AudioStateObserverProtocol and if same, set it to nil so that this cell does not get the updates again.
One important thing to keep in mind here is that, if your audio is still playing then you need to make sure that when the cell for that index is getting created it show updates, this is when you will check the isPlaying state of the datasource and if it is true set the cell as the observer of type AudioStateObserverProtocol

Saving state of UISwitch of cells in UITableView to pass data to source controller

I have a ListTableView with cells organized in several sections.
Another FilterTableView (filter settings for the ListTableView) provided by another controller. Every cell of FilterTableView has a UISwitch to set the showing/hiding state of a specified section of ListTableView.
How exactly do I save the BOOL state of UISwitch?
What kind of model do I need to provide?
What is the right way to pass the state of UISwitch of cells of
FilterTableView to show/hide sections in ListTableView?
Thanks for your help in advance!
If you are using a stroyboard you can create an unwind segue (to go back from FilterTableViewCtrl to ListTableViewCtrl). This case you can save data in the unwind action method which is located in ListTableViewCtrl.
- (IBAction)unwindToList:(UIStoryboardSegue*)unwindSegue
{
UIViewController* sourceViewController = unwindSegue.sourceViewController;
if ([sourceViewController isKindOfClass:[FilterTableViewCtrl class]])
{
FilterTableViewCtrl* filter = (FilterTableViewCtrl*)unwindSegue.sourceViewController;
//read state of filter, and update list (self)
}
}
More details in: View Controller Programming Guide for iOS / Creating an Unwind Segue
You need to either pass a reference to the item in your data source to the cell so it can change it directly (for example, pass in a Filter object), or you need to make the FilterTableView a delegate of each cell and have the cell notify its delegate when the switch value changes.
If the cell is generic and you also use it in other places for other things, go with the delegate pattern. But if it's unique to this table view, it's probably easier just to pass it the Filter object. That way it can also set its label and switch state itself, rather than requiring the data source to set its values.

Setting content inside of a UITableViewCell from outside source?

I have a UITableViewController with a few UITableViewCells. Each of those cells have different content: labels, images, text fields, etc. I am needing to set those pieces of content based around user interaction of my UITableViewController class.
For example: I have a cell that has a UIImageView in it. When a user taps the UIImageView I want a UIActionSheet to show. The problem is that I can't set the UIImageView as a property of the UITableViewController class. It has to be a property of a subclass for that UITableViewCell. But that means if I write the code for showing a UIActionSheet in the UITableViewCell subclass it won't show in the UITableViewController class.
My question is, since all of the objects within a UITableViewCell are in their own class, how do I alert the UITableViewController class when one of those actions happens?
Also, when a UIActionSheet is displayed it will need to show up in my UITableViewController class. If I select an item from that action sheet and wanted the text of that item to propagate to setting the text of a UILabel inside of a UITableViewCell, how could I do that?
With UITableViewController you control everything through the UITableViewDataSource implementation.
Here is how you can do it: make an int field in your model class that says which row needs to show a cell with an action sheet, and set it to -1 (which means that no cell needs to show an action sheet). In the tap handler of the UIImageView call the model, and tell it that the cell to which the UIImageView belongs needs an action sheet now. At that point you tell your UITableView to reload data. This sets the whole system in motion again - the table view calls back your data source to ask for the count, and then it calls again for the cell. This is when your data source looks at the model, sees that the row needs an action sheet, and returns a subclass of the cell with the action sheet visible.
Here is a diagram showing this sequence of events.
When the user is done with the action sheet, the action sheet needs to call the model again, and tell it that the action sheet is no longer needed (i.e. set the index back to -1). After that it should call the table view again, and tell it to reload the data. The sequence will repeat again, but is time there will be no flag asking for the action sheet, so a regular cell would be returned.
well, I simply suggest to use Custom UITableView Cell class, so when you are creating custom cell pass your uitableview controller to that class as parent. Now in your Custom Class you can add singleTap on UIImageView, from tap on it you can call method in your parent class via parent object you have already passed.

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.

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