UISearchBar, NSPredicate and NSMutableArray - nsdictionary

Have next Table View:
image capture screen
this its make with two NSMutableArrays from Sqlite Database, sublista and subdetail, in example element 0 of sublista contain "A Barda" and element 0 of subdetail contain "Ponteceso".
This code generate this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"subCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.accessoryType =UITableViewCellAccessoryDetailButton;
}
NSString *title;
cell.textLabel.text=[sublista objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[subdetail objectAtIndex:indexPath.row];
return cell;
}
Need Add A UIsearch bar and create NSDictionary:
NSMutableDictionary *playas = [NSMutableDictionary dictionaryWithObjectsAndKeys:sublista, #"nombreplaya", subdetail, #"localidadplaya", nil];
But How implement Search or filter Here?
BEST REGARDS

Related

NSUserDefaults + NSMutableArray -> Storing 2 arrays and display in tableview

I have just implementet a bookmark/favorites function in my app, favorites from a tableview, with the following code, using NSUserDefaults. The cell has 2 labels, the name of the item and a price - stored in the Arrays - theArray and thePriceArray -> like this:
NSIndexPath *indexPath = [_ListTableView indexPathForCell:(UITableViewCell *) [[sender superview] superview]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *OrderList = [[defaults objectForKey:ORDER_KEY] mutableCopy];
if (!OrderList) OrderList = [NSMutableArray array];
[OrderList addObject:[theArray objectAtIndex:indexPath.row]];
[OrderList addObject:[thePriceArray objectAtIndex:indexPath.row]];
[defaults setObject:OrderList forKey:ORDER_KEY];
[defaults synchronize];
I am now adding the two arrays theArray & thePriceArray, to the NSMutableArray.I now want to show these information (the information from the arraies) in another tableview. I do this like so:
In My viewDidAppear:
NSUserDefaults *defaults = kSettings;
NSMutableArray *theOrderList = [NSMutableArray arrayWithArray:[defaults objectForKey:ORDER_KEY]];
And to show the contents in the tableview:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [theOrderList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"TheTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [theOrderList objectAtIndex:indexPath.row];
return cell;
}
This is almost working as I want, the contents of theArray and thePriceArray is shown, but in a long list like so:
I want the "key and the value" to be in one cell, so the price and the item name in one cell (Hat - 30) and not seperate, how can I do that? I have tried to use a NSDictionary, but without luck and can I use NSDictionary for this?
You can create two new arrays for favItem and favPrize. Just add your favorite Items to favItem Array and add favorite Prizes to favPrize Array. Now use these arrays to set the labels and detailLabels of your Tableview like :
cell.textLabel.text = [favItems objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [favPrize objectAtIndex:indexPath.row];
Keep both array theArray and thePriceArray seperate and use one as your main array for data source of tableView.
Note : add data from NSUserDefault to respective array.
Now method will be :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [theArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"TheTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubTile reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [theArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [thePriceArray objectAtIndex:indexPath.row];
return cell;
}
You can simply do it using the following code:
NSString *label = [NSString stringWithFormat:#"%# %#",[theOrderList objectAtIndex:(indexPath.row * 2)],[theOrderList objectAtIndex:(indexPath.row * 2)+1]
cell.textLabel.text = label;
And also change your numberOfRowsInSection like:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [theOrderList count]/2;
}

Parsing HTML Data in UITableView

I have an HTML Data which looks like:
<div id="foo"><a class="someClass" href="http://somelink">Some Title</a></div>
<div id="foo"><a class="someClass" href="http://somelink1">Some Title 1</a></div>
<div id="foo"><a class="someClass" href="http://somelink2">Some Title 2</a></div>
I'm able to show Link title in the tableView, but how can I get URL of these specific title to load a new view (which is pushed through navigation controller upon selecting table cell) ?
Here's the code I'm using to parse HTML Data:
HTMLParser * parser = [[HTMLParser alloc] initWithData:responseData error:&error];
HTMLNode * bodyNode = [parser body];
someArray = [[bodyNode findChildrenWithAttribute:#"id" matchingName:#"someID" allowPartial:NO] retain];
And this one to show the array data in the table:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [someArray count];
}
- (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.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
HTMLNode* someNode = [someArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[someNode allContents]];
return cell;
}
When you get a node and want information about a child node within it, you have to continue parsing to find the child node and then get the attribute you are looking for. For example:
HTMLNode *anchorNode = [someNode findChildTag:#"a"];
NSString *anchorHref = [anchorNode getAttributeNamed:#"href"];
It might help to mention that you are looking at Ben Reeves' HTMLParser. (At least I think that is the one you are using).

How to print the array value in cells lable?

I am developing a application in that I want to print the array value in the cell contain label place. I got the vlaues for array like names=[results valueForKey:#"name"].
I write the code for print that value in a lable in cellForRowAtIndexPath like
-(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] autorelease];
}
NSInteger row=indexPath.row;
cell = [self getCellContentView:CellIdentifier];
UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
lblTemp1.text = [names objectAtIndex:row];
//lblTemp2.text = [names objectAtIndex:row];
return cell;
}
but ,when the application got the error at lblTemp1.text = [names objectAtIndex:row] like Program received signal: “EXC_BAD_ACCESS”.So please tell me how to solve this problem.
It seems you've missed to retain the array names. Try,
names = [[results valueForKey:#"name"] retain];
You could consider using declared properties to overcome the overhead of retaining and releasing the objects.
seems to be new bee. please check out for step by step tutorial
http://adeem.me/blog/2009/05/19/iphone-programming-tutorial-part-1-uitableview-using-nsarray/

Reading plist into TableView

I started this project with a simple plist of a dictionary with two arrays of strings. I now want to add more information and want to use this plist structure:
Root - Dictionary - (2 items)
Standard - Array - (3 items)
Item 0 - Dictionary - (4 items)
Color - String - Red
Rvalue - String - 255
Gvalue - String - 0
Bvalue - String - 0
Sorry about typing in the plist but the site would not let me post an image
I know that the RGB values could be numbers instead of strings but I have a reason for them being strings.
This is the code I used to read the simple plist:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *colorSection = [colors objectForKey:key];
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if(cell == nil){
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: SectionsTableIdentifier] autorelease];
}
cell.textLabel.text = [colorSection objectAtIndex:row];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; //add disclosure button to rows
return cell;
}
My question is what is the specific code to retrieve the contents of the color dictionaries to get the Colors for the cell.textLabel.text and also read the RGB values to add a subtitle. I've been working on this for several days and have read references and lots of examples and unfortunately can't solve the problem. Your assistance will be greatly appreciated.
So providing you have your Standard - Array stored against a Array you've defined in your .h file then something like this would work. In this example the array is stored against self.coloursArray.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if(cell == nil){
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: SectionsTableIdentifier] autorelease];
}
NSString* ColourString = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:#"Colour"];
NSString* rValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:#"Rvalue"];
NSString* gValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:#"Gvalue"];
NSString* bValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:#"Bvalue"];
cell.textLabel.text = ColourString;
NSString* subCellString = [NSString stringWithFormat:#"%#:%#:%#", rValue, gValue, bValue];
}
Hopefully that'll give a a hand.
First, do not use -[UITableViewCell initWithFrame:reuseIdentifier:]. It has been deprecated and will give you a warning, plus it will make your subtitle harder to implement. This code is a modified version of yours which loads the information, sets the title to the Color property, and sets the subtitle to a string containing the Rvalue, Gvalue, and Bvalue properties.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *colorSection = [colors objectForKey:key];
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if(cell == nil) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: SectionsTableIdentifier] autorelease];
}
NSDictionary *color = [colorSection objectAtIndex:row];
cell.textLabel.text = [color objectForKey:#"Color"];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#, %#, %#",[color objectForKey:#"Rvalue"],[color objectForKey:#"Gvalue"],[color objectForKey:#"Bvalue"]];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; //add disclosure button to rows
return cell;
}

Objective C - Problem with NSMutableArray and NSTableView

I have a class named Person and in this class is the property PersonName (amongst others).
A MutableArray MyUserInfoArr contains many Person objects.
I want to list each PersonName in a Cell of a TableView? How do I do this?
Thanks in advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Person *myPerson;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
NSString *cellValue = [myUserInfoArr objectAtIndex:indexPath.row];
cell.text = cellValue;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
Person *myPerson = [myUserInfoArr objectAtIndex:indexPath.row];
cell.textLabel.text = myPerson.DisplayName;
NSLog(#"abc %i",indexPath.section);
}
This is my code, it ran, but it just return the attribute Person.DisplayName of the last object Person in the myUserInfoArr. How to fix it?
The easiest way would be to just set the text with:
Person *person = [MyUserInfoArr objectAtIndex:[indexPath indexAtPosition:1]];
cell.textLabel.text = person.PersonName;
indexPath contains two indexes, first is the section index, the 2nd is the item's index within the section. So I'm assuming in your case you have a single section, that's why there is nothing to do with the indexAtPosition:0.
You also have to set up the table's data source methods, these tell your table view how many sections/rows it should show:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [MyUserInfoArr count];
}
Person *person = [myUserInfoArr objectAtIndex:indexPath.row];
cell.textLabel.text = person.PersonName;
The reason your code doesn't work: the text property needs to be an NSString *, not a Person *.