I've just implemented a UITableView controller and for some reason the refresh control isn't showing up at the top is there something that I've got wrong?
ScoresNotificationsPage
- (id)initWithFrame:(CGRect)frame {
self = [self initWithStyle:UITableViewStylePlain];
self.view.backgroundColor = [UIColor whiteColor];
self.view.frame = frame;
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.tintColor = [UIColor redColor];
// Assign the refresh control to the table view controllers refresh property.
[refreshControl addTarget:self action:#selector(changeSorting) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
}
MainViewController
Adding the ScoresNotificationsPage with
[self.view addSubview:scoresNotificationsPage.view];
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.
in part of my app (storyboard) I have a table view with various rows, and when I select a row, an html file should be displayed. Instead when I select a row, I get a blank screen instead.
I thought I needed another view controller after the tableview controller, but I get the same issue whether I have one or not.
In my table controller .m file I have the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
articleIndex = indexPath.row;
WebView *selectedArticle = [[WebView alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:selectedArticle animated:YES];
[selectedArticle release];
That is allowing me to select a row and the screen changes to a blank one, rather than loading the relevant html file.
I have in my resources folder a .h and .m file as follows:
webView.h
#import <UIKit/UIKit.h>
#import "globals.h"
#interface WebView : UIViewController{
NSArray *articleNames;
IBOutlet UIWebView *webView;
}
#property(nonatomic, retain)IBOutlet UIWebView *webView;
#end
webView.m
#import "WebView.h"
#implementation WebView
#synthesize webView;
- (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
- (void)viewDidLoad
{
[super viewDidLoad];
articleNames = [[NSArray arrayWithObjects:
#"chapter1",
#"chapter2",
#"chapter3",
#"chapter4",
#"chapter5",
#"chapter6",
nil] retain];
NSString *chapterName = [[NSString alloc] initWithString:[articleNames objectAtIndex:articleIndex]];
NSString *path = [[NSBundle mainBundle] pathForResource:chapterName ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[chapterName release];
}
- (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 YES;
}
#end
I can't see what I have done wrong here, any advice? I'm not sure how to get the html files to display or where I need to make changes to allow it. If I remove the HTML files from the project the app crashes so it's definitely loading them but for some reason not displaying...
Well first off where is "articleIndex" I see you set it in your tableViewController but it is not webView.h or webView.m. I would suggest first adding in a NSLog
NSURL *url = [NSURL fileURLWithPath:path];
NSLog(#"Article URL:%#", url);
This will let you see if the url is correct first. Second it looks like you connected your UIWebView with interface builder, so make sure that the webView is connect correctly. You can also test the webView buy using:
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: #"http://www.apple.com"]];
And see if apple.com loads. If it does then you know your issue is with your html files or the paths to them.
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?