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.
Related
I have created a game using SpriteKit (GameScene and GameViewController) and Objective-C. Everything there works fine.
Within the same App I have created a UIViewController and a second viewController in storyboard that uses CollectionView. I have the collectionView loading the array.
I want to be able to tap on a collectionView Cell and have it open the gameView. Which I can do. However I would like to pass some information from the collectionView to the GameScene so I can load the background image and other information to individualize the GameScene for each collectionViewCell.
Here is my code for the collectionView
#import "collectionViewController.h"
#import "GameScene.h"
#import "GameViewController.h"
#implementation collectionViewController
#synthesize animalArray,theImage,StringPicture,myCell;
-(void) viewDidLoad {
[super viewDidLoad];
animalArray = [NSArray arrayWithObjects:#"Cat.png",
#"image1.png",
#"image2.png",
#"image3.png",
,nil];
}
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return animalArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
myCell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
UIImage *images;
long row = [indexPath row];
images = [UIImage imageNamed:animalArray[row]];
myCell.image1.image = images;
return myCell;
}
-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"indexPath.row:%ld",(long)indexPath.row);
if (indexPath.row==1) {
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];
}
if (indexPath.row==2) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"GameSceneOne"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
}
}
I have tried assigning the GameScene.image to the vc.image but one is a GameView and the other is a UIViewController so I can't do that.
I have tried using a segue but still the information can't be passed from on to the other.
How should I do this?
I approached this in a different way. Basically:
Cell has the skview and loads and presents scene.
Scene exposes properties (like a label for the months in the example)
In the cellForItemAt of the collectionView datasource, I instantiate the scene and just use the exposed properties.
Here is in code:
Step 1:
class GameCell: UICollectionViewCell {
#IBOutlet weak var myskview: SKView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
if let scene = SKScene(fileNamed: "MyScene") as? MyScene{
scene.scaleMode = .aspectFill
myskview.presentScene(scene)
}
}
}
Step 2:
class MyScene: SKScene {
var gameLabel: SKLabelNode!
var monthValue: String?
var bird: SKSpriteNode?
var floor: SKSpriteNode?
var bgColor: UIColor?
let birdCategory: UInt32 = 0x1 << 0
let floorCategory: UInt32 = 0x1 << 1
override func didMove(to view: SKView) { }
Step 3:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GameCell", for: indexPath) as! GameCell
let scene = cell.myskview.scene as! MyScene
scene.gameLabel.text = monthComponents[indexPath.row]
Just don't forget to create the label inside the scene using:
gameLabel = scene?.childNode(withName: "gameLabel") as! SKLabelNode
You can check the final result in my git: https://github.com/tennydesign/spriteKitAndCollectionView
Just pardon my dust. =)
Couple of ways to do this. One of them is using NSUserDefaults.
To store data in NSUserDefaults, do this:
// object can be almost anything. Strings, arrays, dictionaries...
NSString *object = #"WellDone";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:object forKey:#"Hamburger"];
[defaults synchronize];
To read NSUserDefaults, do this:
NSString *myString = [[NSUserDefaults standardUserDefaults] objectForKey:#"Hamburger"];
The key being that you use the same key as when you created it.
And of course, read the docs (blah,blah, blah).
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'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];
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 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];
}