I would want the showEventDate method to have the data passed in like the one for the tableview below, how can I do that ? I can't put a indexPath in that method. Thanks.
-(void)showEventDate
{
EventView *rvc = [[EventView alloc]initWithNibName:#"EventView" bundle:nil];
[navController pushViewController:rvc animated:YES];
[rvc release];
}
// Display a details screen for the selected holiday/row.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Event *holiday = [dataSource holidayAtIndexPath:indexPath];
HolidaysDetailViewController *vc = [[[HolidaysDetailViewController alloc] initWithHoliday:holiday] autorelease];
[navController pushViewController:vc animated:YES];
}
i assume that you want indexRow to be passed from a UITableView to showEventDate. i'm gonna take a stab at it.. u first implement the delegate method, didSelectRowAtIndexPath:. then pass 'indexPath' into wherever you want: ie, showEventdate.
-(void)showEventPath: (NSIndexPath *)indexPath
{
// your code here...
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self showEventPath:indexPath];
}
Related
project type: masterview application components first view: Tableview second view :webview what i want is to navigate in such a technique that : when user taps a cell, it should get the href property and to open in next webview, the page it links to.. instead of only href property
suggestion thanks
#import "MasterViewController.h"
#import "TFHpple.h"
#import "TFHppleElement.h"
#import "Preface.h"
#import "Chapters.h"
#import "Index.h"
#import "DetailViewController.h"
#interface MasterViewController ()
{
NSMutableArray * preface;
NSMutableArray * chapters;
NSMutableArray * index;
}
#end
#implementation MasterViewController
- (void) loadChapters
{
///// capturing the link /////
/*
NSURL pointer to store the file url
*/
NSURL * chapter_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test/mainpage" ofType:#"html"] isDirectory:NO];
NSData * chapter_data = [NSData dataWithContentsOfURL:chapter_url];
///// passing to TFHpple /////
TFHpple * chapter_parser = [TFHpple hppleWithHTMLData:chapter_data];
///// String for XPath Query /////
NSString * chapter_query_string = #"//div[#class='cD']/ul[2]/li/a";
NSArray * chapter_nodes = [chapter_parser searchWithXPathQuery:chapter_query_string];
///// Array for initializing elements of preface/////
NSMutableArray * chapter_contents = [[NSMutableArray alloc]initWithCapacity:0];
///// Now looping to fetch contents /////
for (TFHppleElement * element in chapter_nodes) {
Chapters * chapter_ptr =[[Chapters alloc]init];
[chapter_contents addObject:chapter_ptr];
chapter_ptr.chapter_title = [[element firstChild]content];
chapter_ptr.chapter_url = [element objectForKey:#"href"];
}
chapters= chapter_contents;
[self.tableView reloadData];
}
viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadPreface];
[self loadChapters];
[self loadIndex];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case 0:
return #"Table of Contents";
break;
case 1:
return #"Chapters";
break;
case 3:
return #"Index";
break;
}
return nil;
}
Method returning no of rows in a section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return preface.count;
break;
case 1:
return chapters.count;
break;
case 2:
return index.count;
break;
}
return 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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
switch (indexPath.section) {
case 0:
{
Preface *preface_access = [preface objectAtIndex:indexPath.row];
cell.textLabel.text = preface_access.preface_title;
return cell;
}
case 1:
{
Chapters * chapter_access = [chapters objectAtIndex:indexPath.row];
cell.textLabel.text = chapter_access.chapter_title;
return cell;
}
case 2:
{
Index * index_access = [index objectAtIndex:indexPath.row];
cell.textLabel.text = index_access.index_title;
return cell;
}
default:
break;
}
return cell;
i want to do something like this
in detailviewcontroller.m
it is the 2nd view controller
this is pseudo code I am weak in OOP concepts so appologies
- (void)viewDidLoad
{
NSMutableArray * contentsfromprevious = [[NSMutableArray alloc]initWithCapacity:0];
Preface * data = [[Preface alloc]init];
data.preface_url = contentsfromprevious;
data = preface_contents;
[self.loadwebview.request];
}
First of all your question is still not clear enough. What I've come to understand that, you want something like this:
In the MasterViewController you wish show a list of your links (html pages stored locally).
If any of the table view cells are tapped, you want to display the locally stored html page in a web view on the DetailViewController.
Assuming this, you should implement this UITableViewDelegate method on your MasterViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailVC.htmlFileName = [htmlFiles objectAtIndex:indexPath.row]; // you might need to change this according to your need, as I said your requirement is unclear
[self.navigationController pushViewController: detailVC animated:YES];
}
Also you'll need to add this into your DetailViewController.h file
#property (nonatomic, strong) NSString *htmlFileName;
And after you've done this, you should change your DetailViewControllers viewDidLoad method into something like this:
- (void)viewDidLoad
{
// your code
// ....
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: htmlFileName ofType:#"html"] isDirectory:NO]]];
webView.scalesPageToFit = YES;
// your code
// ...
}
I am not sure if this is what you really want though. On the other note, you don't have to put break statements after any return statements. It never gets executed.
I am developing an iOS App, I have a tableview controller populated from a MySQL database. When I click on a raw a detailview appears showing data from the database, such as a master detail app. The problem is following:
1. When I run the app on the iPhone simulator, the app shows the three records from the mysql table, thats correct.
2. If I click on the first row, nothing happened.
3. If I click on the second or third row, the app opens the detailview, but the data don't correspond to the selected row(record).
4. I click on the back button of the detailview, and the app opens the masterview again.
5. Now if I click on the first row, the app opens the detailview, but as mentioned before, the detailview data don't correspond to the selected row(record) on the master view.
Here you have my viewcontroller code (master view):
//
// ViewController.m
//
#import "ViewController.h"
#import "DetailViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Areas";
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
NSURL *url = [NSURL URLWithString:#""];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
[data appendData:theData];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[mainTableView reloadData];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:#"Error" message:#"The download could not complete - please make sure you're connect to either 3G or WiFi" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
}
-(int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(int)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [news count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if(cell==nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
}
cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"nombre"];
return cell;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.title =[[news objectAtIndex:indexPath.row]objectForKey:#"nombre"];
detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And here the detailview code:
//
// DetailViewController.m
//
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
#synthesize newsArticle;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
titleLabel.text = [newsArticle objectForKey:#"nombre"];
timeLabel.text = [newsArticle objectForKey:#"name"];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Any idea what I have done wrong??
Thank you
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.title =[[news objectAtIndex:indexPath.row]objectForKey:#"nombre"];
detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
You are using didDeselectRow which is called when a row is deselected and hence the wierd behaviour plus wrong data.. you have to use didSelectRow
Rather than bloat and convolute this post with what I've tried and failed at, I'll just keep it simple, as I'm sure the answer is probably simpler than I think.
I have a scrolling UITableView on the main view of my application. All I'm trying to do is move the default position--or "starting point"--of the scrolling UITableView down about 194 points to make room for my navigation bar and a couple other UI elements. How do I do this? Here are what I believe to be the pertinent method implementations from my ViewController .h and .m files, respectively:
// MGViewController.h
// UITVPractice
#import <Foundation/Foundation.h>
#interface ItemsViewController : UITableViewController
-(id) init;
-(id) initWithStyle:(UITableViewStyle)style;
#end
// MGViewController.m
// UITVPractice
#import "ItemsViewController.h"
#import "MGItem.h"
#import "MGItemStore.h"
#implementation ItemsViewController
-(void)viewDidLoad {
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
self.tableView.backgroundView = backgroundImageView;
[super viewDidLoad];
}
-(id) init {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
/* create 5 random MGItems and place in the MGItemStore */
for (int i = 0; i < 20; i++) {
[[MGItemStore sharedStore] createItem];
}
}
return self;
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [[[MGItemStore sharedStore] allItems] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
/* Create an instance of UITableViewCell */
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"UITableViewCell"];
}
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
/* Display custom background image for cell(s) */
cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"cellBackground.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
/* Display custom background image for selected cell(s) */
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"cellBackgroundTouched.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
/* eliminate the white box that bounds the black text. */
[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];
/* Set the text of the cell to the description of the item that is at the nth index of items, where n = row this cell will appear in on the tableView */
MGItem *p = [[[MGItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[p description]];
[[cell textLabel] setTextColor:[UIColor whiteColor]];
// [[cell textLabel] highlightedTextColor: [UIColor purpleColor]];
return cell;
}
-(id) initWithStyle:(UITableViewStyle)style {
return [self init];
}
#end
I apologize if this post comes off as a "do this for me" post, but I've tried about a dozen different things and none of them have worked. Been stuck on this for about 3 days. Thanks for any help you can provide.
EDIT: Yes, Ismael, you're correct. It is a subclass of UITableViewController. I think I understand what you're saying. Working on it now. Thanks to both who answered.
The best way to do this is to "nest" the tableview controller inside another UIView. If you are using storyboards, just add a "container view" then set that view controller's class to that of your tableview controller. Then, changing the container view's size will automatically change the table view's size. Here's an example where I have a few table views nested in one view:
I take it your controller is a subclass of UITableViewController? If that's the case, then you will have to change that.
In order to do this, change the subclass to UIViewController, add the UITableViewDelegate and UITableViewDataSource protocols.
Then, add a UITableView *tableView property and change your viewDidLoad method to look like this:
-(void)viewDidLoad {
[super viewDidLoad]; // [super viewDidLoad]; should go first!!
CGFloat startingPoint = 194.0; // or whatever
CGRect tableRect = self.view.bounds;
tableRect.origin.y = startingPoint;
tableRect.size.height -= startingPoint;
self.tableView = [[UITableView alloc] initWithFrame:tableRect style:UITableViewStyleGrouped]; // or plain, whichever you need
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; // do this if grouped, looks better!
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.tableView];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
self.tableView.backgroundView = backgroundImageView;
}
Edit: There's a reason for this. In a normal UIViewController, the self.view is a UIView, but in a UITableViewController, the self.view is the same as self.tableView and is a UITableView, and that's why you can't change the frame of it.
I am in troubles :)
I am unable to acheive my project because i cannot pass the data from my UITableView to a DetailView (UIViewController)
I am a beginner so i certainly do something wrong but i don't know what. I have red several tutorials and it seems to be ok ... but it's not !
Here is the .h of my UITableViewController :
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "XMLParser.h"
#import "ColzaDetailViewController.h"
#interface ColzaViewController : UITableViewController <XMLParserDelegate>
{
XMLParser *parser;
NSDictionary *colzaInfos;
}
#property (nonatomic, retain) NSDictionary *colzaInfos;
#end
I have create an NSDictionary to store the data I need to pass to the detailView (ColzaDetailViewController)
Here is the part of my UITalbeViewController .h wich is interresting for my problem :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ColzaDetailViewController *detailViewController = [[ColzaDetailViewController alloc] initWithNibName:#"ColzaDetail" bundle:[NSBundle mainBundle]];
colzaInfos = [parser.stories objectAtIndex:indexPath.row];
detailViewController._colzaInfos = colzaInfos;
[self.navigationController pushViewController:detailViewController animated:YES];
NSLog(#"TEST MainView : %#", detailViewController._colzaInfos);
detailViewController = nil;
}
I think everything is ok here. I have put a NSLog (TEST MainView) to check if there is something in my NSDictionary _colzaInfos.
So here are my .h and .m of my DetailVieuw (UIVIewController)
.h
#import <UIKit/UIKit.h>
#import "ColzaViewController.h"
#interface ColzaDetailViewController : UIViewController
{
IBOutlet UILabel *colzaSettle;
NSDictionary *_colzaInfos;
}
#property (nonatomic, strong) NSDictionary *_colzaInfos;
#property (nonatomic, retain) IBOutlet UILabel *colzaSettle;
#end
.m
#import "ColzaDetailViewController.h"
#implementation ColzaDetailViewController
#synthesize _colzaInfos, colzaSettle;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement loadView to create a view hierarchy programmatically, without using a nib.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
colzaSettle = [_colzaInfos objectForKey:kCloture];
NSLog(#"TEST DetailView : %#", _colzaInfos);
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
As you can see in the viewDidLoad, i have put a second NSLog (TEST DetailView) in order to check one more time if there is something in my Dictionary
And here are the log :
2012-03-14 16:23:54.240 Mobile Settles[7173:f803] TEST DetailView : (null)
2012-03-14 16:23:54.241 Mobile Settles[7173:f803] TEST MainView : {
date = "13/03/2012\n ";
echeance = "Ao\U00fbt 2012\n ";
settle = "453.25\n ";
variation = "5.75";
}
So As you can see the log for DetailView is NULL but in the MainView contains data.
But i need to get those data in the DetailView in order to display them.
The only thing it seem "strange" for me at this step is the _colzaInfos Dictionary is not alloc and init at anytime ... But i have try to allocate it and initialize it in the .m of the detailViewController but my log was at this time
TEST DetailView : {}
Someone can help me to understand what i am doing wrong.
(if you need another part of my code to check something ... feel free to ask.)
Thanks a lot for help
As you told me to do, i have put the line of code at this place, please let me know if i am wrong. And as i told in the comment, When i put a breakpoint directly in this part of code and when i Run the program, the breakpoint stop the process AFTER the NSLog. And if i try to alloc/init it before, i have nothing : TEST detailView : { }.
Sorry but it does not work :(
The code :
#implementation ColzaDetailViewController
#synthesize _colzaInfos, colzaSettle;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
if (! _colzaInfos)
_colzaInfos = [[NSDictionary alloc] init];
}
return self;
}
Add this to your initWithNibName method in ColzaDetailViewController.m.
if (! _colzaInfos) _colzaInfos = [[NSDictionary alloc] init];
It is same issue if you're using an NSArray. You have to initialize the variable at some point for it to be able to hold data. You've only declared it in the .h file.
I have an app that I load a GameScene form a CollectionView. I load the view just fine, but when I'm done I can't seem to be able to return to the collectionView.
I have tried
[self dismissModalViewControllerAnimated:YES];
and
[GameScene dismissModalViewControllerAnimated:YES];
and
[collectionViewController dismissModalViewControllerAnimated:YES];
but they all say No Visible #Interface For viewController declares the selector dismissModalViewControllerAnimated:
Here is the code where I load the GameScene from the collectionView
-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row==0) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"GameSceneOne"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
NSLog(#"mycell.image1.image:%#",myCell.image1.image);
[self presentViewController:vc animated:YES completion:NULL];
}}
Any Ideals of how to dismiss the gameScene and return to the CollectionView?