Adding Multiple array into single Array in ios - ios

1) I am working on one application which contains one single tableview and above that tableview there is one horizontal scrollview which contains number of dynamic button eg.button1,button2....
2)On touch of each button json is comming and i am parsing and showing that json in tableview using NSURLSession.eg.One touch of button1 json will come,on touch of button2 different json will come.
3)After that i want to store each button json array into single array and display accordingly.eg. suppose i clicked on button2 json will come and should be stored in array so that in future i retouch that button data should come from stored array.
I've managed point 1 & 2 but not getting any idea how to solve 3 point.
I also added the one image which will give clear understanding about my problem.
Please if any one have idea then reply to this question.Thanks in advance.

You can use NSCache to cache items if you want, it works like an NSDictionary.
// Init NSCache and declare somewhere as property//you must use singleton approach as well, it's just example i have initialized in viewDidLoad
- (void)viewDidLoad
{
self.cache = [NSCache alloc] new];
}
-(void)callApiForKey:(NSString *)buttonName{
id aObject = [self.cache objectForKey:buttonName]
if aObject == nil {
//perform the API call you are talking about
//after the fetch is preformed and you get data...
[self.cache setObject: dataObject forKey:buttonName];
//perform what you need to with said object
} else {
//perform operation with cached object
}
}
USES:
on your button click action call above method with buton text:
yourButton.titleLabel.text.
[self callApiForKey:yourButton.titleLabel.text];
be suer keys must be unique. other wise you may get/set wrong data obviously.
Thanks

-Each button have a unique tag right? Like button 1 have tag 1, button 2 have tag 2 ,button 3 have tag 3,
-you can manage this thing by Global nsmutabledictionary and nsmutableArray.
-if you click on button 1 before api calling you check
if(dictionary valueforkey:#"1") (1mean selected button tag)
{
no need to call api
no need to add array in dictionary
array=[[dictionary valueforkey:#"1"]mutablecopy];(1mean selected button tag)
[tble reloaddata];
}
else
{
api calling
here you can add array in dictionary
[dictionary setobject:yourarray forkey:#"1"];
array=[[dictionary valueforkey:#"1"]mutablecopy];(1mean selected button tag)
[tble reloaddata];
}

Related

How to Empty array in swift on logout

Hi there guys I have movies array in MoviesViewController which has a tableView and where I display tableCells. And I have logout in SidebarMenuController which onTap goes to LogInController. When I hit logout I want to empty the movies array from MoviesViewController and go to LogInController. How to go about this?
I would try creating an Event class that holds the array of movies. Then add an optional event property to MoviesViewController and observe setting it. On sign out, make the array empty, and set the event for the view controller. So the view controller would have something like:
var event: Event? {
didSet {
moviesArray = event?.movies
tableView.reloadData()
if moviesArray.isEmpty {
presentLogin()
}
}
}
This could also be used to populate the array if getting from a service.

Displaying two sets of results in one TableViewController

I know this is a startlingly stupid question, but I can't figure it out. Every answer involves a UISearchBar, which is not what I've got.
I'm trying to display 2 sets of results on one TableViewController.
Results to display
1) everything in my managedObjectContext which is set up in my viewDidLoad
2) a filtered set of results if a predicate is selected.
On MyTableViewController, I have a popover which instantiates when I click a UIBarButtonItem on MyTableViewController. On the popover, I set a predicate on MyTableViewController.
Basically, I'd like to toggle what's displayed and that display toggle is driven by whether my variable is nil (everything displays) or filtered (variable sets a predicate).
Have two NSArray properties allValues and filteredValues. Set up all your delegate/dataSource properties using your filteredValues array.
Next, do something like this when you first get all your data:
self.allValues = [someController fetchAllValues];
self.filteredValues = self.allValues;
[self.myView.tableView reloadData];
Last, alter your filteredValues array whether or not a predicate is selected:
if (self.selectedPredicate) {
self.filteredValues = [self.allValues filteredArrayUsingPredicate:self.selectedPredicate];
} else {
self.filteredValues = self.allValues;
}
[self.myView.tableView reloadData];

Understand TableView and UISegmentedControl

I Need some help to understand how can I filter a information in Uitableview with UISegmentedControl. I have a UITabeView with data that contain two different data, Rec and Dat . I want to load ALL data when start application and separate Deb and Rec when user choose in UISegmentedControl. When I start application I populate 3 Array alls, recs and dats. I show the array alls, and want to change/filter the data when the user change the choose in UisegmentControl. Can you help me please ?
#IBAction func filtroDebitoCredito(sender: AnyObject) {
//when All
if FiltroControlerTable.selectedSegmentIndex == 0 {
// tableView.reloadData() ???
}
//When Creds
if FiltroControlerTable.selectedSegmentIndex == 1 {
// ???
}
//Debs
if FiltroControlerTable.selectedSegmentIndex == 2 {
// ???
}
Tks for help
Actually, you are already there. Your table view displays model data. So when the user changes the value of the segmented control, switch to the correct set of model data and, exactly as you say, tell the table to reloadData(). What I would do is have four arrays: model, all, recs, and dats. The table, let us say, always displays model. So the segmented control would copy, let us say, recs into model and tell the table view to reload!

Change the array of a UITableView

I´m using a table view to display different types of items. The view controller for the table is always the same, ItemsViewController.
I have a menu with buttons for each type of item. Depending on what button the user clicks, a specific array of items must be used to populate the table view.
My solution:
Every item has an typeId property. When a type button is clicked, the following method is called:
[self.itemController createItemListForId:buttonPressed.typeId];
where buttonPressed is, well, the pressed menu button.
This is the implementation for createItemListForId:
-(void)createItemListForId:(int)theId{
for (Item *item in self.masterItemList){
if(item.typeId == theId){
[self.itemList addObject:item];
}
}
}
The masterItemList is popluted with all the items at initialization.
My ItemsViewController uses the itemList to populate the cells.
The problem is that itemList is empty when the table view appears, so something not working with this method.
Any ideas on how to get this to work?

UIAutomation: Check if element exists before tapping

We have a iPad app which includes a two-column news reader. The left view contains the list of news of which some link directly to a news and some push another view controller with another list of news. This will also cause a UIButton to be set as the leftBarButtonItem of the navigation bar. If we are on first level, a simple image that cannot be tapped will be the leftBarButtonItem.
My goal is now to have a test that taps every news on the first level. If a news leads to a second level list, it should tap the UIButton in the navigation bar.
How can I check, if the leftBarButtonItem is "tappable"? Since it can be either an image or a button, just calling navigationBar().leftButton().tap() will lead to an error if it's an image.
I'm also using the tuneup library if that's any help.
Almost all elements in UIAutomation could be tapped. It does not matter if it is an Image, View or a Button. You will get an error in case an object you are trying to tap is invalid.
How to check:
if ( navigationBar().leftButton().checkIsValid() )
{
navigationBar().leftButton().tap();
}
else
{
//do what you need.
}
or you can check if an object you are trying to tap is a button, for example (not the best way but it works):
if ( navigationBar().leftButton().toString() == "[object UIAButton]" )
{
navigationBar().leftButton().tap();
}
else
{
//do what you need.
}
checkIsValid() is available for all UI elements. It will return true if an object exists.
toString() will return [object UIAElementNil] if element is invalid or will return [object UIAButton] or [object UIAImage].
Also try to use Apple documentation:
http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Reference/UIAElementClassReference/UIAElement/UIAElement.html
You can simply use
if (navigationBar().leftButton().exists)
{
navigationBar().leftButton().tap();
}
else
{
//do what you need.
}

Resources