Filter Array into sections - ios

I am building an IOS app based on data within an array - I'm struggling with filtering data into rows - a snippet of my array is as follows -
ARRAY Data / Array -
sightsObject *sight2 = [[sightsObject alloc] initWithsiteTitle:#"The Beatles Homes" siteSection:#"beatles" siteType:#"Take a glimpse of the fab 4's childhood homes.." siteSubTitle:#"" siteDescription:#"" siteMapUrl:#"" sitePic:#"fabs"];
sightsObject *sight3 = [[sightsObject alloc] initWithsiteTitle:#"The Beatles Gig Venues" siteSection:#"beatles" siteType:#"" siteSubTitle:#"Its not all about the Cavern..." siteDescription:#"" siteMapUrl:#"" sitePic:#"fabs"];
sightsObject *sight4 = [[sightsObject alloc] initWithsiteTitle:#"The Beates Locations" siteSection:#"beatles" siteType:#"" siteSubTitle:#"Stawberry Fields, Penny Lane, Palm House..." siteDescription:#"" siteMapUrl:#"docks" sitePic:#"fabs"]
sightsObject *sight5 = [[sightsObject alloc] initWithsiteTitle:#"Albert Dock" siteSection:#"dock" siteType:#"" siteSubTitle:#"" siteDescription:#"" siteMapUrl:#"" sitePic:#""];
sightsObject *sight6 = [[sightsObject alloc] initWithsiteTitle:#"Keiths Wine Bar" siteSection:#"Restaurants" siteType:#"" siteSubTitle:#"Classic Eatery on Lark Lane" siteDescription:#"" siteMapUrl:#"" sitePic:#""];
self.sightsArray = [NSArray arrayWithObjects: sight2, sight3, sight4, sight5, sight6,nil];
SightsObject.H
The Header for the SightsObject os as follows -
#import <Foundation/Foundation.h>
#interface sightsObject : NSObject
#property(strong)NSString *siteTitle;
#property(strong)NSString *siteSection;
#property(strong)NSString *siteType;
#property(strong)NSString *siteSubTitle;
#property(strong)NSString *siteDescription;
#property(strong)NSString *siteMapUrl;
#property(strong)UIImage *sitePic;
-(id)initWithsiteTitle:( NSString *)siteTitleD siteSection:(NSString *)siteSectionD siteType:(NSString *)siteTypeD siteSubTitle:(NSString *)siteSubTitleD siteDescription:(NSString *)siteDescriptionD siteMapUrl:(NSString *)siteMapUrlD sitePic:(NSString *)sitePicD;
#end
ROWS
I was unsure how to count the amount of data rows that apply to each section - so this is currently hardcoded -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 8;
}
FILTERING DATA INTO TABLE CELLS
My issue is that I now dont know how to filter the data into rows - I currently have below (i have two cell types in my tableview) - when run the code displays the relevant data in the correct cells - but repeats the same data for every section - how can I filter it correctly?
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =#"sightsCell";
static NSString *CellIdentifierH =#"headerCell";
NSString * intro = #"intro";
NSString * ArtNoP = #"lower";
sightsObject *b = [self.sightsArray objectAtIndex:indexPath.row];
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
UITableViewCell *cell;
if ([b.siteSection isEqualToString:intro]) {
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifierH forIndexPath:indexPath];
HeaderCell *cellHead = (HeaderCell *)cell;
cellHead.selectionStyle = UITableViewCellSelectionStyleNone;
cellHead.sightsText.text = b.siteTitle;
cellHead.textLabel.backgroundColor=[UIColor clearColor];
return cellHead;
}
else{
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
sightsCell *cellSights = (sightsCell *)cell;
cellSights.selectionStyle = UITableViewCellSelectionStyleNone;
cellSights.sightsTitle.text = b.siteTitle;
cellSights.sightsSubTitle.text = b.siteSubTitle;
cell.textLabel.backgroundColor=[UIColor clearColor];
return cellSights;
}
}

I'd change what you're doing. Rather than have one array with a big list of items and a static count of the sections I'd have an array which contains sections, and each section would be another array containing the row items. This could either be created manually (your configuration code would change) or automatically (by iterating over your existing sightsArray and running various predicates on it. The choice really depends on how many items you have and how many / where they will come from in the future.
Once you have that, the number of sections is self.sightsArray.count and the number of rows in each section is [self.sightsArray[section] count] and the row for a section is:
sightsObject *b = self.sightsArray[indexPath.section][indexPath.row];
I would also use indexPath.row == 0 as the indication that you should have an intro row because to organise the sections automatically I would keep all of the siteSection values the same for each section.

Related

How to add items to a side menu bar?

Im making my first app. My app will have a side pop out menu. I have gotten the sliding menu part implemented with a UITableView. Now i want to populate the side menu with text and a image next to it and when one is tapped i want to push another view controller. What would be the best way to do this? Thanks
As user132490 said, you probably want to add rows to your tableView. Here's a tutorial: http://www.appcoda.com/uitableview-tutorial-storyboard-xcode5/. It's for Xcode 5 but still will work. You may find it useful from "Adding Table Data" and below. In fact, before the Summary the tutorial teaches you to add a thumbnail next to the text of each of your cells.
Good luck!
When you want to have an image and text for your table, you have to create a UITableViewCell subclass and declare an image view and label. Image view and label must be created in IB, create a prototype cell and add there the image view and label.
Once you've done that you must have an array of what will be the content of your table. It's like this:
In #interface:
#property NSMutableArray *menuArray;
In your viewDidLoad:
NSDictionary *option1 = [NSDictionary dictionaryWithObjectsAndKeys:#"image1.png",#"image",#"Option1",#"desc", nil];
NSDictionary *option2 = [NSDictionary dictionaryWithObjectsAndKeys:#"image2.png",#"image",#"Option2",#"desc", nil];
NSDictionary *option3 = [NSDictionary dictionaryWithObjectsAndKeys:#"image3.png",#"image",#"Option3",#"desc", nil];
NSDictionary *option4 = [NSDictionary dictionaryWithObjectsAndKeys:#"image4.png",#"image",#"Option4",#"desc", nil];
NSDictionary *option5 = [NSDictionary dictionaryWithObjectsAndKeys:#"image5.png",#"image",#"Option5",#"desc", nil];
//assign NSDictionaries to array
self.menuArray = [NSMutableArray arrayWithObjects:option1,option2,option3,option4,option5, nil];
And then you must have table view delegate methods like this:
#pragma mark Table View delegate methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [menuArray count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//return row height
return 32;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier =#"Cell";
//assign TableCell.h to access custom properties
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//get dictionary from menuArray
NSDictionary * dict = [self.menuArray objectAtIndex:indexPath.row];
UIImage *imageIcon = [UIImage imageNamed:[dict objectForKey:#"image"]];
//set image
[cell.img setImage:imageIcon];
//set the label text
cell.label.text = [dict objectForKey:#"desc"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//research more about pushing another view controller.
//if chosen was option1
if (indexPath.row == 0)
{
NSLog(#"option1");
}
//if chosen was option2
if (indexPath.row == 1)
{
NSLog(#"option2");
}
//if chosen was option3
if (indexPath.row == 2)
{
NSLog(#"option3");
}
// if chosen was option4
if(indexPath.row == 3)
{
NSLog(#"option4");
}
// if chosen is option5
if(indexPath.row == 4)
{
NSLog(#"option5");
}
}
In pushing to other view controller, try to research more because it's a lot more explanation. Try this link: https://github.com/ECSlidingViewController/ECSlidingViewController
But with this codes, you must be able to populate your table and when you select a cell there will be a log. Hope this helps :)

Save random number generated in Table View

My app is currently generating random numbers (see code below). What I want is to save that number once the user hits the "Save" button and show it on a table view.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.clickyButton setTitle:#"Generate" forState:UIControlStateNormal];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)handleButtonClick:(id)sender {
// Generates numbers 1 to 49, without creating duplicate numbers.
NSMutableSet * numberSet = [NSMutableSet setWithCapacity:5];
while ([numberSet count] < 7 ) {
NSNumber * randomNumber = [NSNumber numberWithInt:(arc4random() % 49 + 1)];
[numberSet addObject:randomNumber];
}
NSArray * numbers = [numberSet allObjects];
self.n1.text = [NSString stringWithFormat:#"%#", [numbers objectAtIndex:0]];
self.n2.text = [NSString stringWithFormat:#"%#", [numbers objectAtIndex:2]];
self.n3.text = [NSString stringWithFormat:#"%#", [numbers objectAtIndex:3]];
self.n4.text = [NSString stringWithFormat:#"%#", [numbers objectAtIndex:4]];
self.n5.text = [NSString stringWithFormat:#"%#", [numbers objectAtIndex:5]];
self.n6.text = [NSString stringWithFormat:#"%#", [numbers objectAtIndex:6]];
}
#end
Please explain me how I can save it on a table view. xcode beginner.
You should create a variable that's accessible within the scope of the whole class rather than just the specific -handleButtonClick: method, and then add the generated numbers to that variable - an array, set, etc...
From there, you can implement the table view to read the values from the variable via var[indexPath.row] (assuming it's an array), and display it. You will need to call [tableView reloadData]; once the array has been filled with objects to make sure that the tableview displays the data.
create a NSMutableArray for UITableViewDataSource and cache the number.
when a number created by the user,add this number into NSMutableArray.
reload UITableView and show all numbers.
If you use only one number you should think about displaying it in another UI element, preferably a UILabel I would say.
If you want to use a UITableView you will either have to create it with static cells (e.g. in a Storyboard) or configure the data source and delegate object for it (which doesn't really seem what you want right now, unless maybe if you wanted to display multiple random numbers in a list...)
Before anything you should make the array numbers as a variable. In that way it is much easier than creating n1,n2,n3,.... I will show you how to solve your problem based on an existing numbers NSArray variable defined.
You need to implement the UITableView delegates in your header file. So let's suppose this is your header file after implementing the delegates:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
Then take your tableview (IBOutlet or programatically) and set the dataSource and delegate in the implementation file. You should do this in the viewDidLoad: method like this:
[_tableView setDelegate:self];
[_tableView setDataSource:self];
After you have done this you need to implement the delegate methods for the UITableView. This ones:
This method will tell the Table View how many rows it needs to show. In your case is the size of the NSArray called numbers:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return numbers.count;
}
This method will tell the Table View what to show on each cell (DON'T FORGET TO ASSING THE CELL IDENTIFIER OF THE CELL IN THE INTERFACE BUILDER TO "Cell")
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [numbers objectAtIndex:indexPath.row];
return cell;
}
Use this method if you want to do something when the user touches a cell in the table view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
Finally to add numbers to the list as the user touches the button you just need to add these lines of code to your method triggered by the button:
- (IBAction)handleButtonClick:(id)sender {
// Generates numbers 1 to 49, without creating duplicate numbers.
NSMutableSet * numberSet = [NSMutableSet setWithCapacity:5];
while ([numberSet count] < 7 ) {
NSNumber * randomNumber = [NSNumber numberWithInt:(arc4random() % 49 + 1)];
[numberSet addObject:randomNumber];
}
//In case you want to delete previous numbers
[numbers removeAllObjects];
numbers = [numberSet allObjects];
[_tableView reloadData];
}

Create tableView sections dynamically using array in iPhone

I have app in which I want to show the data in table view sections problem is that I do not know the total sections names as it depends on array. So how to show data for each section in cell for row at index path.
NSArray *test=[NSArray arrayWithObjects:#"June 20",#"July 20","May 1",#"May 10",nil];
So I want to show the data in the tableView sections date wise like if June 20 all the records of June 20 should be shown in one sections and in May 1 all the records for May same goes for all. Any idea how to solve this. Thanks
It's best to create a standard structure like (personally, i'd suggest the following):
Array that contains multiple dictionaries
Each dictionary contains 2 keys
Day is a key and, say, June 20, is it's value
Events is a key and, it's value is an array object
This array is a collection of strings that will basically be the content for these days
Example:
Possible UITableViewController subclass methods
- (void)viewDidLoad
{
[super viewDidLoad];
arrTest = #[
#{#"Day":#"June 20",
#"Events":#[#"Repair window pane", #"Buy food for Elephant", #"Pay credit card dues"]},
#{#"Day":#"July 20",
#"Events":#[#"Repair window frame", #"Buy alot more food for Elephant", #"Pay credit card dues dues"]},
#{#"Day":#"May 1",
#"Events":#[#"Replace entire window", #"Sell Elephant, Get Cat", #"Return credit card"]},
#{#"Day":#"May 10",
#"Events":#[#"Take bath", #"Shave", #"Get new credit card"]}
];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//get count of main array (basically how many days)
return arrTest.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//get count of number of events in a particular day
//old style
//return [[[arrTest objectAtIndex:section] objectForKey:#"Events"] count];
return [arrTest[section][#"Events"] count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
//get value of the "Day" key (June 20 / July 20 ...)
//old style
//return [[arrTest objectAtIndex:section] objectForKey:#"Day"];
return arrTest[section][#"Day"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//old style
//NSString *strDayEvent = [[[arrTest objectAtIndex:indexPath.section] objectForKey:#"Events"] objectAtIndex:indexPath.row];
NSString *strDayEvent = arrTest[indexPath.section][#"Events"][indexPath.row];
[cell.textLabel setText:strDayEvent];
return cell;
}
To decide number of section and header title for particular section you need to do implement UITableView delegates methods.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [test count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [test objectAtIndex:section];
}
The section name and the row contents have a relationship...and therefore the most suitable way is to manage it with objects displaying relationship like the requirement
For your case an object/structured array will do the job for you with object has a string content to store the date text and the array storing the cell content data
Or use an NSDictionary with key as date and value as the cell content data as array

How to display values from two tables in uitableviewcell

I'm beginner of Parse.com BaaS. I'm thinking about how to display in one UITableViewCell data from two tables which are connected by pointer. I've got a table named "PackageItems" which contain columns such as:- objectId, packageId, productId, price. In the second table named "SIZES", I store data with attributes like:- objectId, packageItemsId, quantity and sizeTypeId. In the third table named "SIZE_TYPES", I have a columns named:- objectId & typeName. I connect these three tables by pointers in SIZES table like below: packageItemsId -> POINTER -> target class-> PACKAGE_ITEMS sizeTypeId -> POINTER -> target class -> SIZE_TYPES
After that I create a query which return as result every Sizes connected to PackageItems by Pointer packageItemsId.
-(PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:#"Size"];
PFObject *packagedItem = [PFObject objectWithoutDataWithObjectId:#"SxtIpSJ64t"];
[query whereKey:#"packageItemsId" equalTo:packagedItem];
}
In the cellForRowAtIndexPath method, I display quantity from Sizes table like below in the UITextField and also I try to display typeName from table SIZE_TYPES
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
static NSString *simpleTableIdentifier = #"PackageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *sizeLabel = [[UILabel alloc]init];
sizeLabel.text = #"HERE I WOULD LIKE TO DISPLAY VALUE OF SIZE TYPE"
sizeLabel.frame = CGRectMake(20,5,50,34);
[cell.contentView addSubview:sizeLabel];
UITextField *quantityField = [[UITextField alloc]initWithFrame:CGRectMake(245, 5, 55, 34)];
quantityField.borderStyle = UITextBorderStyleRoundedRect;
quantityField.keyboardType = UIKeyboardTypeNumberPad;
quantityField.textAlignment = NSTextAlignmentRight;
quantityField.text = [object objectForKey:#"quantity"];
[cell.contentView addSubview:quantityField];
UILabel *quantityLabel = [[UILabel alloc]init];
quantityLabel.textAlignment = NSTextAlignmentRight;
quantityLabel.text = #"Quantity:";
quantityLabel.frame = CGRectMake(170, 5, 70, 34);
[cell.contentView addSubview:quantityLabel];
}
return cell;
}
I know that the query can return only value from one class. I also tried to create second method that returned SIZE_TYPES but it seems to me that I'm doing it wrong. I've read that the charging of the two table can be done by using the method 'include' but do not really know how to go about it. I also heard about cloud code. Perhaps I should use a relationship and a little change my database schema. Please for any sensible solution.
In your case core data maybe your best choice, checkout this one core data programming guide
and then you can try to use NSFetchRequest and NSPredicate to query the database, that will
give you back a NSArray as return type, then you can sort the result and display them based on
the index.

Selecting Variable Dynamically

Hello and thank you for your time. Im fairly new to IOS/Objective C.
I have multiple variables setup globally at the top of my viewcontroller.
NSMutableArray * A;
NSMutableArray * B;
NSMutableArray * C;
Now when someone selects a cell in a tableview I would like to use the name of that cell to select one of the global variables. I found something for doing this with viewcontrollers, but I need something for misc variables as well. I am reffering to:
id myNewController = [[NSClassFromString(selected) alloc] init];
[[self navigationController] pushViewController:myNewController animated:YES];
So it would be something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Save text of the selected cell:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
stringVariable = cell.textLabel.text;// value=A,B or C
// match string to array and load that array.
??
}
Thanks in advance for any help, and sorry if this has been asked but I couldn't find anything that worked so as a last resort im asking for help :)
You may be better off storing your arrays as keys on a dictionary, instead of as individual fields. For example:
NSDictionary* dictionary;
dictionary = #{
#"A": [[NSMutableArray alloc] init],
#"B": [[NSMutableArray alloc] init],
#"C": [[NSMutableArray alloc] init]
};
Then when your row is selected, you could lookup the array by key:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString* key = cell.textLabel.text;// value=A,B or C
NSMutableArray* array = dictionary[key];
....

Resources