Display objects with specific string value in UITableView - iphone

If I want to display all the objects, I use the code below. But if I only want to display the objects where the value for the key "Favorite" = "Yes", then what? Example of this would be great.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle]
pathForResource:#"Objects" ofType:#"plist"];
sortedObjects = [[NSMutableArray alloc]initWithContentsOfFile:path];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [sortedObjects count];
}
Ps. The Property List is an array with dictionaries.

You need to create a new array, lets call it filtered, that you create by filtering sortedObjects, and use that to populate the table.
NSPredicate *pred = [NSPredicate predicateWithFormat:#"SELF.Favorite == %#", #"Yes"];
NSArray *filtered = [sortedObjects filteredArrayUsingPredicate:pred];

Try adding the following code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
if([[sortedObjects objectAtIndex:indexPath.row] valueForKey:#"Favorite"] isEqualToString:#"YES"])
{
//do stuff
}
}

Related

My custom cell does not display

I have created a custom cell that contain a Label but when I added that in my Table View than it does not display, my view controller is not TableViewController I have seted Table View using IB and seted its delegate and datasource correctly.
I am doing this by:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
ExampleAppDataObject* theDataObject = [self theAppDataObject];
return theDataObject.myAudio.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ExampleAppDataObject* theDataObject = [self theAppDataObject];
NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];
NSArray *parts = [destinationPath componentsSeparatedByString:#"/"];
NSString *filename = [parts lastObject];
AudioInfoCell *cell = (AudioInfoCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.audioName.text = filename;
return cell;
}
First check how many row set in tableView:numberOfRowsInSection method
After set the custom cell like bellow..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AudioInfoCell *cell = (AudioInfoCell *) [tableView dequeueReusableCellWithIdentifier:nil];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"AudioInfoCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (AudioInfoCell *) currentObject;
break;
}
}
}
ExampleAppDataObject* theDataObject = [self theAppDataObject];
NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];
NSArray *parts = [destinationPath componentsSeparatedByString:#"/"];
NSString *filename = [parts lastObject];
cell.audioName.text = filename;
return cell;
}
Two things to check:
Make sure the reuse identifier matches what is in Interface Builder
Make sure your numberOfRowsInSection and numberOfSectionsInTableView
return the right numbers
Also - I don't use:
if (cell == nil) {
cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
for instantiating the cell. That should be taken care of in the AudioCell TableViewCell subclass you have implemented. An example of how I implement this kind of custom cell is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"FlightPickerCell";
FlightPickerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.tripIDLabel.text = #"Some TripID";
cell.hospitalLabel.text = #"Some Hospital";
cell.cityLabel.text = #"Some City";
return cell;
}

Correct Approach to Populating a Table View Using An Array?

I'm currently trying to implement a Table View on my iPhone app that grabs an array from one table view/class and using this array to populate a table in another seperate view. Here is the method I use that adds an exercise to an array if it is clicked/tapped:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
NSUInteger *index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
[array insertObject:str atIndex:index];
self.workout = array;
[array release];
}
Once the save button is pressed, this array will be stored in an array of workouts (arrays) that I want to be populated in another view. Am I taking the correct approach this?
Here is my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier ];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SectionsTableIdentifier ] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
you probably do not want to re-initialize self.workout with a new array each time.
-(void) viewDidLoad
{
self.workout = [[NSMutableArray alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
[self.workout insertObject:str atIndex:0];
}

UITableView: use object from dictionary as title on detail view

I have the following code to make a table from string turned into a dictionary:
- (void)viewDidLoad {
[super viewDidLoad];
testArray = [[NSArray alloc] init];
NSString *testString = #"Sam|26,Hannah|22,Adam|30,Carlie|32";
testArray = [testString componentsSeparatedByString:#","];
dict = [NSMutableDictionary dictionary];
for (NSString *s in testArray) {
testArray2 = [s componentsSeparatedByString:#"|"];
[dict setObject:[testArray2 objectAtIndex:1] forKey:[testArray2 objectAtIndex:0]];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
if (testArray.count >indexPath.row) {
cell.textLabel.text = [[dict allKeys] objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [dict objectForKey:cell.textLabel.text];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
What I would like is for the selected row title to be set as the title on my detail view.
I tried with:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.detailController.title = [[dict allKeys] objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:self.detailController animated:YES];
}
But I get an "EXC_BAD_ACCESS" error.
It works fine if I use #"1" as title, it's just something with my dictionary call that's wrong, I assume.
Make dict a retained dictionary instead of an autoreleased one.
I.E. declare it maybe like this:
dict = [[NSMutableDictionary alloc] initWithCapacity: [testArray count]];
in your viewDidLoad method. Make sure to release it when viewDidUnload is called.
Also, make sure of the number of keys in your dict before calling:
self.detailController.title = [[dict allKeys] objectAtIndex:[indexPath row]];
So, I would do:
if(dict && ([[dict allKeys] count] > [indexPath row])
{
self.detailController.title =
[[dict allKeys] objectAtIndex:[indexPath row]];
} else {
self.detailController.title = #"Here's a problem";
}
Did you implement these UITableView delegate methods ? All these are needed. Also can you post more detailed StackTrace.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [resultSet count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 280.0;
}

How To Structure Array Into UITableView?

I have an array with the following format:
[NSArray arrayWithObjects:
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
nil];
I want to structure this data to load into my tableView.
Each row of the tableview should correspond to each row of the array, then the title of each cell should correspond to the subarray objectAtIndex 2 for the name.
Suppose your array is named myData:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [myData 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] autorelease];
}
// Configure the cell...
NSArray *obj = [myData objectAtIndex:indexPath.row];
cell.textLabel.text = (NSString*)[obj objectAtIndex:1]; //note: 0=>number; 1=>name,..
return cell;
}
In favor of reusability, I would suggest to replace the subarrays with NSDictionaries so that you can get e.g. the name by calling [dict objectForKey:#"name"].

Not able to access array with ids in another view controller in iphone

these is some code from my project..
for (index=0; index<[feed count]; index++) {
//NSLog(#"........%#",[feed objectAtIndex:2]);
NSString* forName=[feed objectAtIndex:index];
NSString *nameString = [forName valueForKey:#"name"];
NSLog(#"--------%#",nameString);
[nameArray addObject:nameString];
NSString *idString = [forName valueForKey:#"id"];
NSLog(#"--------%#",idString);
[idArray addObject:idString];
}
MyFriends *detailViewController = [[MyFriends alloc] initWithNibName:#"MyFriends" bundle:nil];
// ...
// Pass the selected object to the new view controller.
detailViewController.fbNames=nameArray;
detailViewController.fbIds=idArray;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
My problem is in another controller when I am accessing array with names which is fbNames in another class it is showing me proper names,But when I am accessing fbIds which is the array with Ids ...they are showing me null..
how to correct this?
here...in this class ...fbNames are working fbIds are not..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [fbNames count];
}
// Customize the appearance of table view cells.
- (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];
}
// Configure the cell...
cell.textLabel.text=[fbNames objectAtIndex:indexPath.row];
NSLog(#"%#",cell.textLabel.text);
return cell;
}
NSString* forName=[feed objectAtIndex:index];
NSString *nameString = [forName valueForKey:#"name"];
I don't think NSString has valueForKey: