Make buttons populate a UITableView when pressed - ios

I have created a simple rss reader that populates the tableviewcell on viewdidLoad without a problem. My problem is whenever I click my button the tableview doesn't change. I tried reload with the button touchup inside and nothing still happens. Fairly new to Xcode and iOS programming so any help is great.
.m file
#import "ViewController.h"
#import "KMXMLParser.h"
#import "WebViewController.h"
#import "SportsViewController.h"
#interface ViewController ()
#end
#implementation ViewController
{
NSArray *loadData;
NSURL *thumbnails;
}
#synthesize parseResults=_parseResults;
- (void)viewDidLoad
{
[super viewDidLoad];
KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:#"http://www.daytonastate.edu/rss/it.xml" delegate:nil];
_parseResults = [parser posts];
[self refreshFeed];
// Do any additional setup after loading the view, typically from a nib.
}
-(void) refreshInvoked:(id)sender forState:(UIControlState)state
{
[self refreshFeed];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)refreshFeed
{
KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:#"http://www.daytonastate.edu/rss/it.xml" delegate:nil];
_parseResults = [parser posts];
}
- (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 self.parseResults.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"DataCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}// Configure the cell...
cell.textLabel.text = (self.parseResults)[indexPath.row][#"title"];
cell.detailTextLabel.text = (self.parseResults)[indexPath.row][#"summary"];
[self reloadData];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
WebViewController *vc = [[WebViewController alloc] init];
vc.url = [NSURL URLWithString:(self.parseResults)[indexPath.row][#"link"] ];
[self.navigationController pushViewController:vc animated:YES];
//Makes sure function clicks to reader feed.
// Navigation logic may go here. Create and push another view controller.
}
-(IBAction)pressBtn:(id)sender
{
UIButton *button= [UIButton buttonWithType:UIButtonTypeRoundedRect];
if (button.tag==1)
{
NSLog(#"Press button 1");
KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:#"http://www.daytonastate.edu/rss/cea.xml" delegate:nil];
_parseResults = [parser posts];
[self reloadData];
}
if (button.tag==2)
{
NSLog(#"Press button 2");
[self EventsBtn:nil];
}
}
- (IBAction)NewsBtn:(id)sender
{
KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:#"http://rss.cnn.com/rss/cnn_topstories.rss" delegate:nil];
_parseResults = [parser posts];
NSLog(#"reload happened");
// self.printMessage = [[PrintHello alloc] init]; // EDIT: THIS LINE WAS MISSING NOW IT WORKS
//[self.printMessage Print];
NSLog(#"NewsBtn Pressed");
}
- (IBAction)SportsBtn:(id)sender
{
KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:#"http://www.daytonastate.edu/rss/it.xml" delegate:nil];
_parseResults = [parser posts];
}
- (IBAction)EventsBtn:(id)sender
{
KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:#"http://www.daytonastate.edu/rss/events.xml" delegate:nil];
_parseResults = [parser posts];
NSLog(#"eventsBtn Pressed");
}
- (IBAction)WeatherBtn:(id)sender
{
KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:#"http://w1.weather.gov/xml/current_obs/KDAB.rss" delegate:nil];
_parseResults = [parser posts];
}
#end

In your pressBtn: method, replace this line:
UIButton *button= [UIButton buttonWithType:UIButtonTypeRoundedRect];
with:
UIButton *button = (UIButton *)sender;
This assumes that in IB you have already hooked up the pressBtn: method to the appropriate button. The code you had was creating a new button when your pressBtn: action was called. Since this new button has a tag value of 0, nothing happened. The code I propose actually makes use of the button that was tapped by the user.

Using the invoker (sender), that is the item which you had action on it. Dont create a new button, you want to check the tag of button so the new button is not the item for your work.
Try your best man, :).

Related

Tableview data won't load when unsegmented control is changed

//The tableview is not changing. I have been at this for days. It seems so simple. Thank you for your help
#import "StopsTableViewController.h"
static NSString *MyIdentifier = #"RouteListing";
#interface StopsTableViewController ()
#property (strong, nonatomic) NSArray *TESTARRAY;
#end
#implementation StopsTableViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//Set the tab bar item's title
self.title = #"Stops";
}
self.stopList = [[API sharedAPI] fetchStopListing];
self.TESTARRAY = #[#"Josh", #"Kyle", #"Nate"];
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MyIdentifier];
// Adds Segmented Control
[self segmentedView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) segmentedView {
NSArray *segmentedMenu = [NSArray arrayWithObjects:#"All", #"Near Me", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedMenu];
[segmentedControl addTarget:self
action:#selector(valueChanged:)
forControlEvents:UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0;
self.navigationItem.titleView = segmentedControl;
}
pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([self.segmentedControl selectedSegmentIndex] == 0) {
return [self.stopList count];
}
else {
return [self.TESTARRAY count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (_segmentedControl.selectedSegmentIndex == 0) {
cell.textLabel.text = [[self.stopList objectAtIndex:indexPath.row] objectForKey:#"stoptitle"];
cell.detailTextLabel.text = [NSString stringWithFormat:#"Stop %#", [[self.stopList objectAtIndex:indexPath.row] objectForKey:#"stopnumber"]];
}
else {
cell.textLabel.text = self.TESTARRAY[indexPath.row];
}
return cell;
}
pragma mark - Table view delegate
// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here, for example:
// Create the next view controller.
StopInfoViewController *detailViewController = [[StopInfoViewController alloc] initWithNibName:#"StopInfoViewController" bundle:nil];
// Pass the selected object to the new view controller.
detailViewController.stopInfo = [[self.stopList objectAtIndex:indexPath.row] objectForKey:#"stopnumber"];
detailViewController.stopName = [[self.stopList objectAtIndex:indexPath.row] objectForKey:#"stoptitle"];
// Push the view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
//Function for segmentedView
-(void) valueChanged:(UISegmentedControl *)sender {
[self.tableView reloadData];
NSLog(#"I'm getting called segment number is: %ld", (long)sender.selectedSegmentIndex);
}
#end
Check your datasource array before calling the table reload method and make sure that the array contains new values corresponding to the segment that you have selected.
One thing I always run into when struggle up a table view is connecting the table with the interface using interface builder. Because it sounds like you're getting the data loaded, when the view first loads. But when you call [self.tableView reloadData] without having the connection made to the table, nothing will happen.
I ways forget that super simple step. Hope this helped
Did you set the tableview delegates?
Try putting this in your viewDidLoad function.
[tableView setDelegate:self];
[tableView setDataSource:self];
If you want a fuller explanation, check here:
how to set a tableview delegate
That post also explains how to ensure your class subscribes to the UITableViewDelegate and UITableViewDataSource protocols
Hope that helps.

List not being populated from other view

I have a view controller where the user can populate an item. Upon exiting the view that object should be saved, and loaded by the previous view which contains a list of all these objects. My NSLog shows that the object is being saved, but I can't get it to appear in the list view.
Code for both controllers is below. Thanks
List Controller
#import "ItemsViewController.h"
#import "Calculation.h"
#import "CalculationItemStore.h"
#import "CalculationDetailViewController.h"
#interface ItemsViewController()
#property UISegmentedControl *segment;
#end
#implementation ItemsViewController
- (instancetype)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
UINavigationItem *navItem = self.navigationItem;
navItem.title = #"MACS";
// Create a new bar button item that will send
// addNewItem: to CalculationsViewController
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addNewItem:)];
// Set this bar button item as the right item in the navigationItem
navItem.rightBarButtonItem = bbi;
navItem.leftBarButtonItem = self.editButtonItem;
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)viewDidLoad
{
self.segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Publication", #"About", nil]];
self.tableView.tableHeaderView = _segment;
[_segment addTarget:self action:#selector(segmentPressed:) forControlEvents:UIControlEventValueChanged];
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:#"UITableViewCell"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[CalculationItemStore sharedStore] allCalculations] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get a new or recycled cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell" forIndexPath:indexPath];
// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the tableview
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *item = items[indexPath.row];
cell.textLabel.text = item.title;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CalculationDetailViewController *detailViewController = [[CalculationDetailViewController alloc] init];
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *selectedItem = items[indexPath.row];
// Give detail view controller a pointer to the item object in row
detailViewController.calculation = selectedItem;
// Push it onto the top of the navigation controller's stack
[self.navigationController pushViewController:detailViewController
animated:YES];
}
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If the table view is asking to commit a delete command...
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *item = items[indexPath.row];
[[CalculationItemStore sharedStore] removeItem:item];
// Also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void) tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath
{
[[CalculationItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row
toIndex:destinationIndexPath.row];
}
- (void)segmentPressed:(id)sender {
if (_segment.selectedSegmentIndex ==0) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://ferm.forestry.oregonstate.edu/facstaff/leshchinsky-ben"]];
}else if(_segment.selectedSegmentIndex ==1){
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [UIImage imageNamed:#"MACSLoad#2x.png"]];
[self.view addSubview: imageView];
sleep(5);
imageView.hidden = YES;
}
}
- (IBAction)addNewItem:(id)sender
{
// Create a new Calculation and add it to the store
Calculation *newItem = [[CalculationItemStore sharedStore] createCalculation];
CalculationDetailViewController *detailViewController = [[CalculationDetailViewController alloc]initForNewItem:YES];
detailViewController.calculation = newItem;
detailViewController.dismissBlock = ^{
[self.tableView reloadData];
};
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:detailViewController];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
// [self presentViewController:navController animated:YES completion:NULL];
[self.navigationController pushViewController:detailViewController animated:YES];
}
#end
Item controller save method
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//clear responder
[self.view endEditing:YES];
//save changes
BOOL success = [[CalculationItemStore sharedStore]saveChanges];
if(success){
NSLog(#"Saved all calcs");
}else{
NSLog(#"failure saving");
}
}
I think the issue is that by the time the table reloadData is called on the first controller, the second one is not done loading. You could notify the first VC to reload the data once the second one is done saving It can be notified using
create a delegate on the second one or
using notifications
Let me know if you need more details on how to do this!
Looks like there is also an issue with the object creation.
your calculation elements are not getting created properly. When you are ready to save.. look at the contents of self.privateItems. They values filled in are nil. You will need to instantiate the Strings properly.
engineerName needs to be alloc'd and other strings as well.
and when user hits Done, the values from the text boxes need to be set to the your data model.

UISearchDisplayController delegate methods not being called

I am trying to gain some knowledge on UISearchDisplayController and going through some tutorial with examples, I have the below class ready with a search bar and table view with data on it.
Header file:
#import <UIKit/UIKit.h>
#interface MyClass : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>
#property (nonatomic, strong) IBOutlet UITableView *suggestionsTableView;
#property (nonatomic, strong) IBOutlet UISearchBar *searchBar;
#end
Implementation file:
#import "MyClass.h"
#interface DVWNoteTypeSuggestionDisplayController ()
#property (nonatomic, strong) NSArray *items;
#property (nonatomic)BOOL isSearching;
#property (nonatomic, strong) NSMutableArray *filteredList;
#end
#implementation MyClass
- (id)init
{
self = [super initWithNibName:#"SuggestionDisplayController" bundle:BUNDLE];
if (self)
{
// Set the title.
self.title = #"test";
}
return self;
}
- (void)viewDidLoad
{
self.searchBar = [[UISearchBar alloc] init];
[[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.delegate = self;
self.searchBar.frame = CGRectMake(0, 0, 0, 38);
self.suggestionsTableView.tableHeaderView = self.searchBar;
self.items = [[NSArray alloc] initWithObjects:#"Item No. 1", #"Item No. 2", #"Item No. 3", #"Item No. 4", #"Item No. 5", #"Item No. 6", nil];
self.isSearching = NO;
self.filteredList = [[NSMutableArray alloc] init];
}
#pragma mark - Table view data source
- (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.
// // Usually the number of items in your array (the one that holds your list)
// return [self.items count];
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (self.isSearching)
{
//If the user is searching, use the list in our filteredList array.
return [self.filteredList count];
} else
{
return [self.items count];
}
}
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// //Where we configure the cell in each row
//
// static NSString *CellIdentifier = #"Cell";
// UITableViewCell *cell;
//
// cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// }
// // Configure the cell... setting the text of our cell's label
// cell.textLabel.text = [self.items objectAtIndex:indexPath.row];
// return cell;
//}
- (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];
}
// Configure the cell...
NSString *title;
if (self.isSearching && [self.filteredList count]) {
//If the user is searching, use the list in our filteredList array.
title = [self.filteredList objectAtIndex:indexPath.row];
} else
{
title = [self.items objectAtIndex:indexPath.row];
}
cell.textLabel.text = title;
return cell;
}
- (void)filterListForSearchText:(NSString *)searchText
{
[self.filteredList removeAllObjects]; //clears the array from all the string objects it might contain from the previous searches
for (NSString *title in self.items) {
NSRange nameRange = [title rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (nameRange.location != NSNotFound) {
[self.filteredList addObject:title];
}
}
}
#pragma mark - UISearchDisplayControllerDelegate
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
//When the user taps the search bar, this means that the controller will begin searching.
self.isSearching = YES;
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
//When the user taps the Cancel Button, or anywhere aside from the view.
self.isSearching = NO;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterListForSearchText:searchString]; // The method we made in step 7
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterListForSearchText:[self.searchDisplayController.searchBar text]]; // The method we made in step 7
// Return YES to cause the search result table view to be reloaded.
return YES;
}
#end
Whenever I try to search for any items in the data set (eg: "Item No. 5") its not hitting the breakpoints on any of the delegate i.e. actually the search is not working. Please suggest what am I missing here as this is just my learning project right now.
It appears that a UISearchDisplayController created programatically in this way is prematurely zeroing the delegate, even though the view controller is retaining the UISearchDisplayController as expected.
Add the following to the end of your viewDidLoad and you'll see that the first log will be a valid object, but the second will be null.
NSLog(#"Delegate should be %#", self.searchDisplayController.delegate);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(#"Delegate is %#", self.searchDisplayController.delegate);
});
The quickest way I've found to get around this is to store your own reference to the UISearchDisplayController via a private property or ivar.
If you have to create object for search bar controller and set delegate search bar its work.
UISearchBar *searchBar = [[UISearchBar alloc] init];
[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.delegate = self;
searchBar.frame = CGRectMake(0, 0, 0, 38);
self.tableView.tableHeaderView = searchBar;

Sending a data model between UITableViewControllers

I know there are already lots of questions about this but none of them have helped me yet. I have two UITableViewControllers. I want to push a QuicklistViewController (using this like a quick menu to list different shows) onto a CurrentShowViewController.
I have a Show model that only contains a name property right now. I am have trouble moving the data from my selection in QuicklistViewController to the CurrentShowViewController so that I can display the currentShow information there.
I am totally stuck on how I can do this.Please help!
Here is my CurrentShowViewController:
#import "CurrentShowViewController.h"
#import "QuicklistViewController.h"
#interface CurrentShowViewController ()
#end
#implementation CurrentShowViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
self.tabBarItem.image = [UIImage imageNamed:#"tab_icon_episodes.png"];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Add a UIBarButton button that will display a Quicklist Modal View
UIBarButtonItem *quicklistButton = [[UIBarButtonItem alloc] initWithTitle:#"Quicklist"
style:UIBarButtonItemStylePlain
target:self
action:#selector(quicklistButtonPressed)];
self.navigationItem.leftBarButtonItem = quicklistButton;
self.currentShow = [[Show alloc] init];
NSLog(#"Current show name is: %#", self.currentShow.name);
self.title = self.currentShow.name;
}
- (void) viewWillAppear:(BOOL)animated {
self.title = self.currentShow.name;
}
- (void) quicklistButtonPressed {
QuicklistViewController *quicklistVC = [[QuicklistViewController alloc] init];
[self.navigationController pushViewController:quicklistVC animated:YES];
}
Here is my QuicklistViewController:
#import "QuicklistViewController.h"
#import "CurrentShowViewController.h"
#interface QuicklistViewController ()
#end
#implementation QuicklistViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
self.title = #"Quicklist";
self.tabBarItem.image = [UIImage imageNamed:#"tab_icon_quicklist.png"];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backButtonPressed)];
self.navigationItem.leftBarButtonItem = backButton;
// Create a temp array of Show objects filled with test data
NSMutableArray *temp= [[NSMutableArray alloc] init];
[temp addObject:[[Show alloc] initWithName:#"The Walking Dead"]];
[temp addObject:[[Show alloc] initWithName:#"How I Met Your Mother"]];
[temp addObject:[[Show alloc] initWithName:#"Grey's Anatomy"]];
[temp addObject:[[Show alloc] initWithName:#"The Mentalist"]];
[temp addObject:[[Show alloc] initWithName:#"Stargate SG1"]];
NSArray *testShows = [[NSArray alloc] initWithArray:temp];
self.shows = testShows;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.currentShow = [[Show alloc] init];
}
- (void) backButtonPressed {
//Archive changes to currentShow object
[Show saveShow:self.currentShow];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (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 [self.shows count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
}
cell.textLabel.text = [self.shows[indexPath.row] name];
return cell;
}
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Change the current show to the selected show
self.currentShow = self.shows[indexPath.row];
//Archive changes to currentShow object
[Show saveShow:self.currentShow];
}
Use delegation.
CurrentShowViewController
- (void) quicklistButtonPressed
{
QuicklistViewController *quicklistVC = [[QuicklistViewController alloc] init];
quicklistVC.delegate = self;
[self.navigationController pushViewController:quicklistVC animated:YES];
}
- (void) selectedShow:(Show) show
{
// show : current show
}
QuicklistViewController
#protocol QuicklistViewControllerDelegate
- (void) selectedShow:(Show) show
#end
#property (nonatomic, weak) id delegate;
- (void) backButtonPressed
{
//Archive changes to currentShow object
[Show saveShow:self.currentShow];
[delegate selectedShow:self.currentShow];
[self.navigationController popViewControllerAnimated:YES];
}
You've mistakenly implemented didDeselectRowAtIndexPath instead of didSelectRowAtIndexPath.

How to show different view controller based on RowSelected in Tableviewcell?

I am new to iPhone Development and trying to create a small application(Recipe list) with multiple views. When the user selects one of the rows in tablecell, I would like to call a new view. For Instance, the menu card with items mushroom, pasta,steak must call another view with detailed recipe.
I have used UITableViewController to display a list of items with Accessory. These items are dynamically populated from a NSArray. When the user taps on one of the rows in the table, it should take him to a view controller based on the Row Selection. I know I have to implement tableView:didSelectRowAtIndexPath: function to call the appropriate view.
But I am not able to proceed. I read about Segue in storyboards, but even that seems a bit confusing for me.
I would really appreciate if someone solves my query. Here is my code.
RecipeViewController.h
#import "RecipeViewController.h"
#import "Recipe.h"
#interface RecipeViewController ()
#end
#implementation RecipeViewController
{
NSArray *recipes; //Recipe Array
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"Hotel";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
// Create recipe array for main screen.
Recipe *recipe1 = [Recipe new];
recipe1.headline = #"Mushroom";
recipe1.description = #"description";
recipe1.imageFile = #"mushroom.jpg";
Recipe *recipe2 = [Recipe new];
recipe2.headline = #"pasta";
recipe2.description = #"Pasta description";
recipe2.imageFile = #"pasta.jpg";
Recipe *recipe3 = [Recipe new];
recipe3.headline = #"Steak";
recipe3.description = #"Steak description";
recipe3.imageFile = #"steak.jpg";
recipes = [NSArray arrayWithObjects:recipe1, recipe2, recipe3,nil];
// Remove table cell separator
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
// Assign custom backgroud for the view
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"welcome_screen"]];
self.tableView.backgroundColor = [UIColor clearColor];
// Add padding to the top of the table view
UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
self.tableView.contentInset = inset;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (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 recipes.count;
}
- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
NSInteger rowIndex = indexPath.row;
UIImage *background = nil;
if (rowIndex == 0) {
background = [UIImage imageNamed:#"cell_top.png"];
} else if (rowIndex == rowCount - 1) {
background = [UIImage imageNamed:#"cell_bottom.png"];
} else {
background = [UIImage imageNamed:#"cell_middle.png"];
}
return background;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Display recipe in the table cell
Recipe *recipe = [recipes objectAtIndex:indexPath.row];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:recipe.imageFile];
UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
recipeNameLabel.text = recipe.headline;
UILabel *recipeDetailLabel = (UILabel *)[cell viewWithTag:102];
recipeDetailLabel.text = recipe.description;
// Assign our own background image for the cell
UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
cell.backgroundView = cellBackgroundView;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CampusViewController *campusViewController;
WhoViewController *whoViewController;
switch (indexPath.row)
{
case 0:
campusViewController=[[CampusViewController alloc] init];
[[self navigationController] pushViewController:campusViewController animated:YES];
break;
case 1:
whoViewController=[[WhoViewController alloc] init]; // not released as ARC is included in the project
[[self navigationController] pushViewController:whoViewController animated:YES];
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowDetails"]) {
RecipeViewController *detailViewController = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
/* NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
detailViewController.data = [self.dataController objectInListAtIndex:indexPath.row];
*/
}
}
#end
// Recipe.h
#import <Foundation/Foundation.h>
#interface Recipe : NSObject
#property (nonatomic, strong) NSString *headline; // name of recipe
#property (nonatomic, strong) NSString *description; // recipe detail
#property (nonatomic, strong) NSString *imageFile; // image filename of recipe
#end
Have you read any tutorials on how to do this? This is a very basic question and there are a lot of posts about this out there.
I'll only give you an explanation on how to do it but you will have to code it yourself.
The standard way we go about an application that shows the user different view controllers based on the selection of a UITableViewCell is by using a UINavigatorController. This controller gives you the methods:
– pushViewController:animated:
– popViewControllerAnimated:
... among others. With these two methods you can go back and forth between view controllers, which is what you want to do. So you have to give your navigator controller the table view controller that you already have. You can do that by using UINavigatorController's method – initWithRootViewController:
A little example of what your – tableView:willSelectRowAtIndexPath: might look like is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *myViewController = [[UIViewController alloc] initWithNibName:#"YourNibName" bundle:nil];
self.navigationController pushViewController:[myViewController autorelease] animated:YES];
}
Hope this helps!
Further reading:
UINavigationController tutorial Ray Wenderlich
UINavigationController tutorial Techotopia

Resources