So i have a table view hooked up to a plist with urls. When I click on the table view cells i want it to take me to the detail view, but the only problem right now is when i click on the cell to take me to the detail view it just loads a dark screen with nothing on it. I don't receive any errors on the compiler so i have no idea what is wrong. I have reset the stimulator and tried restarting my computer but now I'm starting to think there is something wrong with the code. Can someone help?
TableViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSString *myListPath = [[NSBundle mainBundle] pathForResource:#"myList" ofType:#"plist"];
tableData = [[NSArray alloc]initWithContentsOfFile:myListPath];
NSLog(#"%#",tableData); }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [tableData count]; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if( cell == nil )
{
NSLog(#"Cell Creation");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
//Set Data For each Cell
cell.textLabel.text = [[tableData objectAtIndex:indexPath.row]objectForKey:#"cellName"];
cell.detailTextLabel.text = [[tableData objectAtIndex:indexPath.row]objectForKey:#"cellSubtitle"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Index Selected,%d",indexPath.row);
WebViewController *View = [[WebViewController alloc] init];
NSString *urltoPass = [NSString stringWithString:[[tableData objectAtIndex:indexPath.row]objectForKey:#"cellSubtitle"]];
View.urlString = [[NSString alloc] initWithFormat:#"http://%#",urltoPass];
View.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:View animated:YES completion:nil];
}
When you make a controller in a storyboard, you need to get an instance of that view controller by using instantiateViewControllerWithIdentifier:, not by using alloc init.
WebViewController *View = [self.storyboard instantiateViewControllerWithIdentifier:#"WebView"];
In the storyboard, you need to give the controller the identifier "WebView".
Related
this is my UITableView code
in viewDidLoad
self.audioHistoryTableView = [[UITableView alloc] init];
self.audioHistoryTableView.delegate = self;
self.audioHistoryTableView.dataSource = self;
table view
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"numberOfRowsInSection");
NSLog(#"tableeeeeeee %lu",(unsigned long)[audioHistory count]);
return [audioHistory count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cellForRowAtIndexPath");
NSString *CellIdentifier = #"historyCell";
UITableViewCell *cell = (UITableViewCell *)[self.audioHistoryTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *audio = [audioHistory objectAtIndex:indexPath.row];
cell.textLabel.text = audio;
return cell;
}
when I add data and call reload
NSArray *newItem = [NSArray arrayWithObject:#"new audio!"];
NSLog(#"new item : %#",newItem);
[audioHistory addObjectsFromArray:newItem];
[self.audioHistoryTableView reloadData];
I know it's not work because I add new data and change to other page, when back to this page the added data was shown.
Can you set the delegate and datasource via the interface builder instead? Also if it's made in the interface builder why do you have to initialise the table? Remove this:
self.audioHistoryTableView = [[UITableView alloc] init];
self.audioHistoryTableView.delegate = self;
self.audioHistoryTableView.dataSource = self;
then in the interface builder control drag from the UITableView up to the UIViewController and set the delegate to self. And do that again and set the data source to self. Try that
So i created a custom cell (using the .xib file) and linked it using a custom controller class and I also didn't forget to write in the cell identifier. I also gave the same cell identifier in my table view controller prototype cell. In the custom cell controller class I just have an outlet to a text label in the .h file. Below is the code for my table view controller. When I run the app, the custom cells are not displayed but there are cells there because i can click on them (but they are just white). What am I doing wrong, why aren't the custom cells displaying?
If I use the default cells (not custom), then everything works fine. So the problem is that I'm not using my custom cells correctly somewhere.
#import "ListmaniaTableViewController.h"
#import "ListmaniaTableViewCell.h"
#interface ListmaniaTableViewController ()
#property (strong, nonatomic) NSMutableArray *tasks; //of task object
#end
#implementation ListmaniaTableViewController
- (void)viewDidLoad{
[super viewDidLoad];
task *task1 = [[task alloc] init];
task1.taskDescription = #"TASK 1";
[self.tasks addObject:task1];
task *task2 = [[task alloc] init];
task2.taskDescription = #"TASK 2";
[self.tasks addObject:task2];
task *task3 = [[task alloc] init];
task3.taskDescription = #"TASK 3";
[self.tasks addObject:task3];
}
- (NSMutableArray *)tasks{
if(!_tasks){
_tasks = [[NSMutableArray alloc] init];
}
return _tasks;
}
/*- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"Add New Item"]) {
}
}
- (void)addNewTask:(task *)newTask{
[self.tasks addObject:newTask];
}
#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.tasks count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ListmaniaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
if(!cell){
[tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(ListmaniaTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
task *task = [self.tasks objectAtIndex:indexPath.row];
NSString *taskLabel = task.taskDescription;
cell.taskLabel.text = taskLabel;
}
#end
But the
[self.tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];
in viewDidLoad:.
The code you have in tableView:willDisplayCell:forRowAtIndexPath: method should be in tableView:cellForRowAtIndexPath: method. Like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ListmaniaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
if(!cell){
[tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
}
task *task = [self.tasks objectAtIndex:indexPath.row];
NSString *taskLabel = task.taskDescription;
cell.taskLabel.text = taskLabel;
return cell;
}
I figured out the problem. I had an outlet in the custom cell that was doubly linked. I also moved the line below into viewdidload as suggested by #dasdom
[self.tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];
I have a table view which pushes a detailed view on cell click. The new view pushes however I'm trying to pass a web address to the detailed view which loads a webView but I'm unsure on how to do this. Here is my code:
-(void)setupArray {
webAddress = [[NSMutableDictionary alloc]init];
[webAdress setObject:#"http://www.google.com.au" forKey:#"first item"];
[webAdress setObject:#"http://www.yahoo.com.au" forKey:#"second item"];
menuItems = [webAdress allKeys];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [webAdress count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [menuItems objectAtIndex:indexPath.row];
return cell;
}
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
[self.navigationController pushViewController:detailView animated:YES];
}
Any help would be greatly appreciated.
You need to use a property for that in your detail view and pass the URL in the didSelectRowAtIndexPath method like this :
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
detailView.webAddress = [menuItems objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailView animated:YES];
}
and in your DetailView :
#property (nonatomic, strong) NSString *webAddress;
You should add an instance variable to the DetailViewController which will hold the address & set it to the desired value. Then (with the proper accessor) set it before/after pushing the view.
e.g. if you defined the variable as webAddress:
DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
[detailView setWebAddress:[webAddress objectForKey:[menuItems objectAtIndex:[indexPath row]]]]; // or some other way to get the correct address
[self.navigationController pushViewController:detailView animated:YES];
The only thing that remains to be done is to set the web view's address to this value on viewWillAppear: or something.
Edit:
A sample viewWillAppear: method would look like this:
- (void)viewWillAppear:(BOOL)animated
{
// construct request
NSURL *url = [NSURL URLWithString:webAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
[super viewWillAppear:animated];
}
I have created a tableview with the data from an array. Now i want the details for all the rows of the tableview. If i select any recipe from the tableview it should show me all the ingredients and procedure to prepare that recipe with a pic of that particular recipe on top. I am new to this field so kindly help.
Here's "ViewController.m"
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
RecipeList = [[NSMutableArray alloc]init];
[RecipeList addObject:#"Chicken Kabab"];
[RecipeList addObject:#"Chicken Tikka"];
[RecipeList addObject:#"Chicken 65"];
[RecipeList addObject:#"Chicken Do Pyaza"];
CGRect frame = self.view.frame;
RecipeTable = [[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain];
RecipeTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
RecipeTable.backgroundColor = [UIColor cyanColor];
RecipeTable.delegate = self;
RecipeTable.dataSource = self;
[self.view addSubview:RecipeTable];
[RecipeTable setScrollEnabled:YES];
[RecipeTable setUserInteractionEnabled:YES];
[RecipeTable setRowHeight:35];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"RECIPES";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return RecipeList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text = [RecipeList objectAtIndex:indexPath.row];
return cell;
}
#end
Just pass recipe name to the detailViewController. Then depending on the name of the recipe you can load the suitable image in UIImageView.
In the code recipename is an NSString in detailViewController. Declare it as a property..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
NSString *name=[recipe objectAtIndex:indexPath.row];
detailViewController.recipename=name;
[self.navigationController pushViewController:detailViewController animated:YES];
}
Then in the detaiViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
if([recipename isEqualToString:#"Pizza"])
{
UIImage *image = [UIImage imageNamed: #"pizza.png"];
[img setImage:image];
}
else if([recipename isEqualToString:#"Chicken 65"])
{
UIImage *image = [UIImage imageNamed: #"chicken.png"];
[img setImage:image];
}
}
you must use the DidSelectRow Delegate of UITableView . Then if you select a recipe it calls the DidSelectRow delegate .Inside this delegate you do the coding for pushing to a new ViewController . Create an UIImageView on top of your new ViewController , then pass the image from your TableViewCell to the UIImageView of your new viewController
If you want the details of a particular Dish populated in another ViewController that is pushed, the easiest way is by creating an NSObject with all the details required for that particular dish and maintaining it as an array. When you select a row in tableView, the didSelectRowAtIndexPath: get the object at that indexPath and pass it to the next VC which is being pushed. In the detail VC you can populate the details with that from the object.
Instead of doing like this
RecipeList = [[NSMutableArray alloc]init];
[RecipeList addObject:#"Chicken Kabab"];
do something like
RecipeList = [[NSMutableArray alloc]init];
[RecipeList addObject:dishObj];
where dishObj is an NSObject, of Class Dish which contains Dish name, image and recipe.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
Dish *dishObject = [RecipeList objectAtIndex:indexPath.row];
cell.textLabel.text = dishObject.dishName;
return cell;
Try this ,
you must use the DidSelectRow Delegate of UITableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
NSString *strDishName = [RecipeList objectAtIndex:indexPath.row];
detailViewController.strDishDetail =strDishName;
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
I hope the following tutorial will useful for u..Segue
I'm writing a project that simulates a Master/detail layout for iPad.
So i've a masterView(UITableView), a detailView(UICollectionView), a global Navigation Bar and a TabBar.
The tabbar is filled with some catecories i need to choose from and the masterview is filled according to the tabbaritem selected.
In landscape mode, when both master and detail view are shown, everything works fine.
In portrait mode, my master view is hidden and i've got a button that open a popupcontroller with my master in it. The problem is that this popup doesn't seems to show the changes made on the masterview content
the MasterView is a UITableViewController.
I've correctly implemented the delegate and datasource method.
Here is the code to open/dismiss the popover
if(myPopIsVisible)
{
myPop = [[UIPopoverController alloc]initWithContentViewController:myMasterView];
[myPop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
myPopIsVisible = YES;
}
else
{
[myPop dismissPopoverAnimated:YES];
myPopIsVisible = NO;
}
Checking in debug it lend me to the correct content of myMasterView (number and content of rows) but it only show the first one loaded by the app.
I'm using ARC...
This is myMasterView class implementation
#implementation myMasterView{
NSArray *cellTitles;
NSArray *cellIco;
NSArray *cellTag;
}
- (void) setData:(NSArray *)titles :(NSArray *)icos :(NSArray *)tags
{
cellTitles = titles;
cellIco = icos;
cellTag = tags;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
// Configure the cell...
cell.textLabel.text = [cellTitles objectAtIndex:indexPath.row];
cell.tag = [[cellTag objectAtIndex:indexPath.row] integerValue];
#warning icona non impostata
// cell.imageView.image = [cellIco objectAtIndex:indexPath.row];
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [cellTitles count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(isPopover)
{
isPopover = NO;
[master dismissPopoverController];
}
}