Using EGOTableViewPullRefresh together with KKGridView - iphone

Has anyone one ever tried that ? EGOTableViewPullRefresh seems rely on the fact that the parent view is a UITableViewController, but KKGridView is essentially a UIViewController

Actually KKGridView is a UIScrollView, if you are using KKGridView by subclassing KKGridViewController you can make this work by setting the gridView's delegate to self.
if (_refreshHeaderView == nil) {
EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.gridView.bounds.size.height, self.view.frame.size.width, self.gridView.bounds.size.height)];
self.gridView.delegate = self;
view.delegate = self;
[self.gridView addSubview:view];
_refreshHeaderView = view;
[view release];
}

Related

The right way to implement loadView?

I have a question regarding the implementation of loadView:
Right now, I have it like this:
- (void)loadView
{
image = [UIImage imageNamed:#"plan.gif"];
scrollView=[[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0,scrollView.frame.size.width + 40, scrollView.frame.size.height)];
imageView.image = image;
[imageView setContentMode:UIViewContentModeCenter];
scrollView.contentSize = image.size;
scrollView.maximumZoomScale = 3.0;
scrollView.bounces = NO;
scrollView.delegate = self;
// do any further configuration to the scroll view
// add a view, or views, as a subview of the scroll view.
[scrollView addSubview:imageView];
// release scrollView as self.view retains it
self.view=scrollView;
[scrollView release];
}
I suppose some of it should be in viewDidLoad:?
Thanks in advance.
This seems fine to me.
viewDidLoad is normally used as your hook after getting a view returned from IB. In this case you are essentially just doing the work of IB in code (setting up the view heirachy and configuring it).
Therefore in this case I think splitting out the logic may be superfluous unless it makes your code more readable.

App freeze when I remove [super loadView]

When I remove the [super loadView]; the view wont display. The superclass is UIViewController.
- (void)loadView
{
[super loadView];
UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 367.0f) style:UITableViewStylePlain];
tableview.dataSource = self;
tableview.delegate = self;
self.tableView = tableview;
[tableview release];
[self.view addSubview:self.tableView];
}
Any idea why? Thanks in advance!
1) UIViewController Class Reference, loadView section
Your custom implementation of this method should not call super.
2) You have to set view property to something. After all this method is called loadView :). Instead of [self.view addSubview:self.tableView]; try
self.view = tableView;
If you look at the view programming guide, it mentions that if you override [loadView], you should construct your own view.
default loadView will look at bunch of stuff, like load from nib first, then construct normal view.
So, just construct a view, and assign it to self.view -
UIView *view = [[UIView alloc] initWithFrame ...];
self.view = view;
[view release];
then it should be fine.
edit: example with your code:
- (void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 367.0f)];
self.view = view;
[view release];
UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 367.0f) style:UITableViewStylePlain];
tableview.dataSource = self;
tableview.delegate = self;
self.tableView = tableview;
[tableview release];
[self.view addSubview:self.tableView];
}
edit2: link to viewcontroller programming guide:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP40007457-CH101-SW1
Look at custom view controller section, Creating the View Programmatically, and few other places in that doc.
I think you wanna move that [ tableview release ] after the addSubview.
This is because you never assign the view property in the code. When in the last line you access the view property, it causes -loadView to be called again, which results in a endless loop.

Why am i getting a black frame and not the picker?

I have this very simple code or at least i think it's simple.
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
CGRect rect = CGRectMake(0.0f, 20.0f, 320.0f, 216.0f);
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:rect];
self.view = myPickerView;
[myPickerView release];
}
I am running a general View Based template with XCode. I can see the loadView getting called but i get an black frame instead of the UIPickerView.
What am i doing wrong ?
/donnib
Have you implemented the picker's datasource methods? You need it otherwise it won't show.
Link to Apple Documentation here: http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Reference/UIPickerViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIPickerViewDataSource
You've forgotten to set the UIPickerView's delegate to your current view. Add:
myPickerView.delegate = self;
...following your initialization and before your view. And of course, make sure you set up your header file as a UIPickerView delegate and implement the dataSource methods.
Try this:
UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainscreen] bounds]];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame: CGRectZero];
CGSize pickerSize = [pickerView sizeThatFits: CGSizeZero];
pickerView.frame = [self pickerFrameWithSize: pickerSize];
[pickerView setShowsSelectionIndicator: YES];
[contentView addSubview: pickerView];
[pickerView release];
[self setView: contentView];
[contentView release];
Instead of overriding -loadView, you should override -viewDidLoad. Once the view has loaded you'll create the picker and add it as a subview of the view owned by the view controller.
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rect = CGRectMake(0.0f, 20.0f, 320.0f, 216.0f);
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:rect];
[self.view addSubview:myPickerView];
[myPickerView release];
}

Creating a UITableViewController programmatically

This is what I tried. Nothing appears on the screen and none of the UITableView methods that you are supposed to implement are getting called.
-(void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = view;
[view release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITableViewController *TVC = [[[UITableViewController alloc]
initWithStyle:UITableViewStylePlain] autorelease];
CGRect cgRct = CGRectMake(0, 10, 320, 100);
UIView *newView = [[UIView alloc] initWithFrame:cgRct];
TVC.view = newView;
[newView release];
[self.view addSubview:TVC.view];
}
I've looked for good examples and tutorials on doing this programmatically but there are none.
What I am trying to achieve is a Table that doenst take up my who screen. Maybe 3/4 of my screen would be good.
Many Thanks
Code
The problem is that you're creating a UITableViewController, which is a UIViewController, and will expect to be on the nav stack. What you want to do is create a UITableView, which is a UIView. You are also not setting the table's delegate and data source, which you will need to do to get calbacks.
Your viewDidLoad should look something like this
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *table = [[[UITableView alloc] initWithStyle:UITableViewStylePlain] autorelease];
table.dataSource = self;
table.delegate = self;
table.frame = CGRectMake(0, 10, 320, 100);
[self.view addSubview:table];
}
(Note that if you're going to need access to the table outside of the callbacks, you should save it in an ivar rather than declaring it locally, and should retain it. Let me know if you need a few more lines of code to show you what I mean)
Make sure you set the delegate of TVC, with
TVC.delegate = self;
That's the reason why none of those methods are getting called. Also, make sure your class implements the UITableViewDelegate protocol by changing your interface declaration to
#interface YourViewController : UIViewController <UITableViewDelegate> {
//declare variables here
}
Also, equally important, don't set TVC.view, as this already happens when you initialize the view controller. You're just setting it to a blank view, which is why you're not seeing anything.
iOS7 seems to like this way of init'ing the tableview:
//make tableview
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 81, 200, 200) style:UITableViewStylePlain];
table.dataSource = self;
table.delegate = self;
[self.dataView addSubview:table];
try that out. Hope it helps someone.
UIView *newView = [[UIView alloc] initWithFrame:cgRct];
TVC.view = newView;
I'll give you a hint. Here you are setting the view of the UITableViewController to an EMPTY VIEW...

How does autosize work in iPhone SDK?

I'm trying to do the following:
alt text http://img101.imageshack.us/img101/7396/33959221.png
I need to do a lot of custom things, so I subclassed everything.
alt text http://img248.imageshack.us/img248/4201/49422483.png
I'm having issues with autoresize and positioning. For instance the UIToolBar is not at the correct position.
So here is the question:
If you don't use UITableViewController or any other Controller, that takes care of all the positioning and sizing, then you have to do it yourself.
Can someone someone tell me how this works? What do I need to understand?
---- UPDATE:
My custom goals are actually not that important as my question is actually about something else. But OK. So all "elements" like the TableView, the Toolbar and the TabBar will have custom backgrounds and use custom buttons. The TableView will be GroupedStyle and the groups can be collapsed.
My question is how do I have to structure my code especially regarding positioning and autoresizing in order to achieve what you can see in picture #1.
This is how it's structured right now:
Application Delegate:
- (void) loadView {
RootViewController *rootView = [[RootViewController alloc] init];
[window addSubview:rootView.view];
[window makeKeyAndVisible];
RootViewController:
- (void) loadView {
self.theView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.theView.autoresizesSubviews = YES;
self.theView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.view = self.theView;
ChildViewController *childView = [[ChildViewController alloc] init];
[self.view addSubview:childView.view];
ChildViewController:
- (void) loadView {
self.theView = [[UIView alloc] init];
self.view = self.theView;
}
- (void) viewDidLoad {
self.view.frame = [[UIScreen mainScreen] bounds];
CGRect toolbarBounds = self.theToolbar.bounds;
CGRect viewBounds = self.view.bounds;
CGRect tableViewBounds = CGRectMake(0, CGRectGetHeight(toolbarBounds), CGRectGetWidth(viewBounds), CGRectGetHeight(viewBounds) - CGRectGetHeight(toolbarBounds));
self.view.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.theTableView = [[MyTableView alloc] initWithFrame:tableViewBounds
style:UITableViewStyleGrouped
parent:self];
[self.view addSubview:self.theToolbar];
[self.view addSubview:self.theTableView];
}
MyToolBar:
- (id)initWithParent:(id)theParent {
if ((self = [self init])) {
self.parent = theParent;
CGRect rectArea = CGRectMake(0, 0, CGRectGetWidth([[[self parent] view] bounds]), 44);
[self setFrame:rectArea];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self loadButtons];
}
return self;
}
So this is how the positioning is structured at the moment (messy). It actually works, BUT I have a ModalView which is handled by the ChildViewController. When I have the iPad in Portrait view and I pop up the modal view and dismiss it, then everything works. But when I do the same on Landscape view it rotates the hole view... the ChildViewController by 90 degrees when dismissing the modal view. Very strange. When I load directly ChildViewController in my AppDelegate, then again everything works fine.
You're missing the UIViewAutoresizingFlexibleBottomMargin option in the autoresize mask of your toolbar, for instance.