Save each didSelectRowAtIndexPath click's object in MutableArray - ios

I have a list of items/cells in TableView. Table is set to allow multiple selection.
So, i have these methods to select and deselect.
#interface NotificationsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,UIScrollViewDelegate>
{
NSMutableArray *checkedIndexPaths;
}
-(void)viewDidLoad{
//Setup default array
checkedIndexPaths = [[NSMutableArray alloc]init];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imgIcon.image=[UIImage imageNamed:#"select-icon"];
Notifications* selected = notifications[indexPath.section];
selectedGroup = selected.NotificationsId;
[checkedIndexPaths addObject:selectedGroup];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imgIcon.image=[UIImage imageNamed:#""];
Notifications* selected = notifications[indexPath.section];
selectedGroup = selected.NotificationsId;
[checkedIndexPaths addObject:selectedGroup];
}
I want to add each object into array during multiple selection. But with this current coding, it will overwrite the same one, and i have no idea how can i make it multiple object array to store in each click of cell. Thank you for your helps.

You're creating new NSMutableArrays after each method call. Your listArray should be a property of the object, or otherwise visible outside of the scope of these two methods, that you add and remove items from on each method call.

Related

How to use NSUserDefault to send selected TableViewCell text to previous ViewController

I have a static UITableView with two prototype cells in my first UIViewController. in the first cell there is a label with "select your country". When user tap that cell, it goes to another UITableViewController that includes countries. When user select a country, in the first view label text should update with the selected country name. To do that I have to pass the selected data in second UIViewController to first view controller. I hope to use NSUserDefaults to do that. this is my second view controller with a tableview.
#implementation FlightfromTableViewController
{
NSArray *detailFlights;
}
- (void)viewDidLoad {
[super viewDidLoad];
detailFlights = #[#"colombo1",#"colombo2",#"colombo3",#"colombo14",#"colombo15",#"colombo16",#"colombo17"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [detailFlights count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identi" forIndexPath:indexPath];
cell.textLabel.text = [detailFlights objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Pass *p = [Pass new];
p.selectedString = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
NSString *selecteOne = p.selectedString;
[[NSUserDefaults standardUserDefaults] setObject:selecteOne forKey:#"selectedplace"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
I appreciate if you provide answer with understandable code, because I'm new to iOS.
NSUserDefaults should not be used for application state. It's for user preferences which should persist when the app closes.
The correct way is to pass your data directly between your viewcontrollers: https://stackoverflow.com/a/9736559/78496
As suggested by #chedabob, NSUserDefaults should not be used to maintain application state.
Instead, you can use Protocols to back propagate the selection in First View Controller.
In your case, just use
[[NSUserdefaults standardUserDefaults] objectForKey:#"selectedplace"];
in viewWillAppear of FirstViewController to update the string.
First of all - you don't need to much code in your didSelect method to get your selected string. Just use your array:
NSString *selectedText = [detailFlights objectAtIndex: indexPath.row]; // or detailFlights[indexPath.row]
And when you come back to your master (first) ViewController you need to update your TableView, because data has changed. So, add viewWillAppear (if you haven't one) method and refresh your table:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
[self.tableView reloadData]; // put here name of your tableView's property
}
And then, in your cellForRow method:
...
if (indexPath.row == 0) { // in first row you store country
NSString *text = #"Select country"; // default text
NSString *selectedCountry = [[NSUserdefaults standardUserDefaults] objectForKey:#"selectedplace"];
if (selectedCountry) { // if it exist
text = selectedCountry;
}
cell.textLabel.text = text;
}
You can get value from NSUserDefault by using this code in your First View Controller.
NSString *selectedPlace = [[NSUserdefaults standardUserDefaults] objectForKey:#"selectedplace"];
You should use delegate pattern to inform the first view controller about selected country. For this you have to write protocol in second view controller.
#protocol SecondViewControllerDelegate
-(void)secondViewController:(UIViewController *)vc selectedCountry:(NSString *)country;
#end
then in
(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
delegate.secondViewController(self) selectedCountry:#"selectedCountry";
}
After that in first view controller you implement this menthod to update the value.

iOS: I am trying to send the selected rows(data) to another controller.

I have a tableview for which I am using
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
I have an NSArray *selectedDiscounts which I have assigned like this
selectedDiscounts = [self.tableView indexPathsForSelectedRows];
I have to pass the selected table rows data to another controller where I will be populating the tableView with selected rows.
The problem is selectedDiscounts either holds only selected indexPaths and not the data? because of which it shows me the number of objects that are selected but not the data for those selected cells.
I want to store the selected rows data into an NSArray variable. Is that possible? Thanks guys.
You need to iterate through all of your index paths and get the data yourself.
NSMutableArray *array = [[NSMutableArray alloc] init];
for (NSIndexPath *indexPath in selectedDiscounts) {
// Assuming self.data is an array of your data
[array addObject: self.data[indexPath.row]];
}
Now you have your NSArray that contains your data that can you pass to your next controller.
Your selectedDiscounts array is clearly being populated with the UITableView method indexPathForSelectedRows. To store the actual data for the selected rows you need to first establish an array allDiscounts, with which you populate your first table view. Then, when you are displaying all of the objects from allDiscounts and you want to select some and store the data do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[selectedDiscounts addObject:[allDiscounts objectAtIndex:indexPath.row]];
}
The way I would handle this, is to create a custom initializer method on the view controller you want to pass the data to. Something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *selectedDiscounts = yourDataSource[indexPath.row];
NewViewController *newVC = [[NewViewController alloc] initWithSelectedDiscounts:selectedDiscounts];
self.navigationController pushViewController:newVC animated:YES];
}
An alternate method would be create a property on the second view controller that is the array/dictionary you want to pass, and when they select the row, get the information for the row, and set it on the view controller before you push/present it.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *selectedDiscounts = yourDataSource[indexPath.row];
NewViewController *newVC = [[NewViewController alloc] initWith...// whatever you use for the initializer can go here...
newVC.discounts = selectedDiscounts;
self.navigationController pushViewController:newVC animated:YES];
}

Get text from one label on multi-label UITableViewCell

On my UITableViewI am using custom UITableViewCells. Each of these cells has a number of labels. When the user selects a cell, I need to capture the contents of just one of these labels, but I don't know how to do that. Here is my code. The line that I am using to attempt to get this label text is basically pseudo-code that clearly won't compile. Can somebody please tell me what I need to do here? Thanks!
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
Groups *group = [self.fetchedObjects objectAtIndex:indexPath.row];
cell.groupDescriptionLabel.text = group.group_descr;
cell.groupIDLabel.text = [group.group_id stringValue];
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// capture the user selection
Groups *group = [self.fetchedObjects objectAtIndex:indexPath.row];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *selection = selectedCell.groupDescriptionLabel.text; //<-- pseudo-code
NSLog(#"%#", group.group_descr);
...
}
It's generally a bad idea to get data out of the view. You shouldn't really be using any view objects as a way of storing information.
You are already getting the string when you are in the cellForRowAtIndexPath method.
You should be able to do the same in didSelectRowAtIndexPath to get the same string.
That way you don't have to get the text out of the label at all.
Thanks
If you know that celForRowAtIndexPath is going to be returning one of your custom cell types instead of a generic UITableViewCell, cast the result to your custom cell class:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// capture the user selection
MyCellClass *selectedCell = (MyCellClass *) [tableView cellForRowAtIndexPath:indexPath];
NSString *selection = selectedCell.groupDescriptionLabel.text; //<-- pseudo-code
NSLog(#"%#", selection);
//...
}

Get text of all selected rows

I have a UITableView and I am trying to get the text of all the selected rows when a button is pressed. Here's what I have so far:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
[selectedRows addObject:selectedCell.textLabel.text];
}
selectedRows is an array.
Here is my button press function where I need access to the checked row titles:
- (IBAction)selectList:(id)sender {
NSLog(#"%#", selectedRows);
}
This doesn't work for me because if a user unchecks a row and then clicks the submit button - it is not reflected in the array. I would appreciate some help with this. thanks.
Instead of dealing with keeping tabs on what is/isn't selected at any given moment, why not just wait until it's time to select the list to generate it and let the table keep track of what's selected. (provided it isn't some massive amount of data) You could loop through the index paths of your table's selected cells and pull the string you're looking for directly out of the datasource.
- (IBAction)selectList:(id)sender
{
NSMutableArray *arrayOfText = [NSMutableArray new];
for (NSIndexPath *idx in self.tableView.indexPathsForSelectedRows) {
[arrayOfText addObject:dataSource[idx.row]];
}
NSLog(#"%#",arrayOfText);
}
You could add a didDeselectRowAtIndexPath function and remove the item from the array.
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
[selectedRows removeObject:selectedCell.textLabel.text];
}

Selected Index of UI Cell

I am new to iOS programming
I would like to Save the Selected Cell (cell.detailTextLabel.text) to another Textfield outside of table view in the same viewController .
Can anyone suggest me how?
Use this delegate method of the table view,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
selectedLabelName = cell.textLabel.text;
}
declare variable name selectedLabelName in your viewcontroller

Resources