How to form a grid using UITableview? - objective-c

I am working on TV guide project, I want to know how to form a grid i.e I want to split the cells and construct it just like a grid. I know that I have to use UITableview, but I want to know how to do it.
If any one please provide the concept with sample project or code will be appreciated.

You can use custom cells, each cell with some around 4 to 5 buttons based on your requirement.
I hope you have idea on creating custom table cells & using them.
providing code is bit clumsy thing here

I have implemented something like this before, my approach was to subclass UIScrollView and implement some custom view recycling and layout. It was pretty simple to implement, although things got a bit complicated when I started implementing animations. Basically, the idea is to override - (void)layoutSubviews with something like:
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect visibleBounds = [self bounds];
// calculate the range of visible views
// recycle any no longer visible views
// for any missing views, request them from the data source
}
You will need to implement your own data source and delegate protocols for whatever needs you have, mine were pretty simple.

This sort of project already exists look into AQGridView.

The Transporter iPhone app does this in a great way; you can check out their code on GitHub here.
Here's what the tableview looks like in their app:

Related

NSTableView problems - displaying custom TableView from with a SplitView panel

I am developing my first app for OSX. Sorry for asking stupid questions. I have spent a few hours trying to figure this out on my own, with no luck so far.
I want to make an iTunes-like interface. I used NSSplitView, placed NSView for navigation and NSTableView above that. [I am aware that there better alternatives to NSSplitView, yet my goal is to both - develop an app and also to learn Cocoa/OSX in the process.]
Atop NSView panel designated for navigation, I am trying to place NSTableView. However, my table is not being displayed. I therefore have questions...
I understand that for cells to be populated, controller must implement NSTableViewDataSource. I tried that, but was so far unsuccessful - to the point that I don't see the table. Please advise:
Can I have a working NSTableView-derived custom class also implementing NSTableViewDataSource? If this cannot work, please advise why or point me to an explanation.
Am I correct in thinking that all elements can be manipulated programmatically, in the sense that I use IBOutlet in headers to point to the right object, yet do nothing with InterfaceBuilder - have everything controlled from within my Objective-C code? Do I have to use IB?
Thank you.
Yes that will work but it's an unusual approach. Generally the tableview delegate/datasource is something enclosing the tableview. You'd normally only subclass NSTableView if you require some additional functionality not provided by default (for me that has been custom behaviour to input).
Yes you can do it all programmatically, however you will find it much easier to use IB. The IB-loaded views are created programmatically under the hood, using the information contained in the nib file. You will find it long-winded and tedious pretty quickly.
WRT to your issue with not seeing the table, you will need to add logging/breakpoints on the few key delegate/datasource methods to ensure they are being called (start with the daddy of them all numberOfRowsInTableView:). If they are not then you aren't setting the delegate/datasource correctly in the tableview.

How can I do an animated switch to UISplitView?

I have looked at the sample generated by xcode when creating a new UISplitView app on the iPad along with countless other tutorials and the documentation from the apple developer site. I have not seen an example where the UISplitView used was not the root of the application. Is this even possible?
What I am trying to accomplish: I have a UITableView to start out and once an item in the list is selected I would like to display a splitview with two different sets of information that is based on the item that was selected.
I curious if this type of implementation is even possible, or just frowned upon, and why. If it is possible, how would I go about implementing and hooking up a UISplitView to behave in this way?
Edit: I'm updating this with what I have. I can now switch to my UISplitView, though the transition is not animated. What is the way to correctly switch to a UISplitView so the transition is animated?
Code for switching right now:
[appDelegate.window addSubview:appDelegate.splitViewController.view];
appDelegate.window.rootViewController = appDelegate.splitViewController;
EDIT 2: In hopes of bumping this back up so more people see it, I have managed to switch from my navigationController to my splitViewController, but when I add the button to be able to navigate back, nothing I do makes a difference and I seem to be locked in. I tried reverse mirroring the code to switch to the splitViewController, but that had no affect, and I am completely out of ideas. Can anyone shed some light on this?
You should always use SplitViewController as a rootViewController: Split view controller must be root view controller
There may be some hacks around it, but when Apple have a strong recommendation and design guidance, I suggest to try to re-think your design before going against the platform -it should save you effort in the long term.
I recommend using the MGSplitViewController, it also works as a non-rootViewController, even nested into an another MGSplitViewController, and there's i.e. a one-liner for the animation to blend in the Master-View, if that is what you want.
In your UITableView didSelectRowAtIndexPath method you would have something like:
UISplitViewController *mySplitView = [[UISplitViewController alloc] init];
[self.navigationController pushViewController:mySplitView animated:YES];
[mySplitView release];
Probably you'll want to subclass UISplitViewController just like you would other view controllers and set in there the master and detail views and so on.

Display horizontal grid lines in nstable view using cocoa

hi i am working with NSTableView in my app.
I want to display grid lines depending on the number of rows but it shows many lines even when the number of rows are very less.
Is this a usual behavior?
Or am i doing something wrong?
I have checked the horizontal grid lines option from xib.
cannot understand how to achieve this using code.
What I found to work best for me so far is the following code.
Just fool the original grid drawing code to draw only on populated rows.
Subclass NSTableView, if needed and override drawGridInClipRect:(NSRect)clipRect as following:
- (void)drawGridInClipRect:(NSRect)clipRect
{
NSRect lastRowRect = [self rectOfRow:[self numberOfRows]-1];
NSRect myClipRect = NSMakeRect(0, 0, lastRowRect.size.width, NSMaxY(lastRowRect));
NSRect finalClipRect = NSIntersectionRect(clipRect, myClipRect);
[super drawGridInClipRect:finalClipRect];
}
If I understood your issue I may say: "YES". It's expected from the NSTableView to be fulfilled of stripes even when empty if you set it so.
I realize that you want also to manage those lines programmatically. Consider check out this method setGridStyleMask: on the NSTableView Class Reference.
Good luck.

Passing Object Between View Controllers in Objective C

I have a very simple iPhone view based application I need help on. It's a view based application with a nav. bar in the footer that switches between 4 view controllers.
What I need to do is pass a UILabel value from view 2 to view 4. The UILabel field is a value calculated in view 2, but I want it to appear in view 4 (if 5+5=10, I want the 10 to appear in view controller 4, not view controller 2).
How do I go about doing this? Does anyone have any sample code I can review? I've searched awhile in Apple's docs and online and haven't found anything helpful yet. Keep in mind I'm a real newb. when it comes to development. I'm just starting to learn!
Thanks in advance.
There are a few ways. I would probably just have a variable created in the application delegate's interface and just change it and access it there.
NSObject *myVariableFromDelegate = [[[UIApplication sharedApplication] delegate] myVariable];
[[[UIApplication sharedApplication] delegate] setMyVariable:10];
Take a look at NSNotificationCenter. You can send a messages and handle them anywhere in the app, best solution in most similar cases.
The application delegate might work for you, but if you are going to have a lot of values to keep track of, you are probably better off implementing a separate class for your data. Otherwise it will get unwieldy quick.
When you load a new view, you pass off a pointer to the data cass to that view controller so that the current values can be pulled out and put into the correct fields. In addition, you could use register for notifications in the view controller to catch any changes that other views make. Just make sure you de-register the notification when the view unloads, and you should be good.

Looking for info on custom drawing of interface components (Cocoa)

It seems like more and more OS X apps these days are doing all kinds of fancy drawing stuff for custom controls. Apps like Twitterific, Things, EventBox, Versions just to name a few....
So basically I'm looking for any information on how to get started doing this kind of thing. Not sure if it is just done by subclassing controls and using custom drawing or if it is something entirely different.
Any help is greatly appreciated. THanks!
It depends entirely on what you want to do.
The "Show Raw Properties" button in Versions for instance is an NSButton subclass, because basically what we needed is standard button behavior with our own look.  One way to subclass a button is to simply implement your own -drawRect:(NSRect)rect method in the NSButton subclass, but we decided to stick with the way NSButton is implemented in Cocoa, meaning most drawing is done by the button's cell, so the implementation looks like this:
In the NSButton subclass:
+ (Class) cellClass
{
return [OurButtonCell class];
}
- (void)drawRect:(NSRect)rect
{
// first get the cell to draw inside our bounds
// then draw a focus ring if that's appropriate
}
In the NSButtonCell subclass (OurButtonCell):
- (void)drawInteriorWithFrame: (NSRect) rect inView: (NSView *) controlView
{
// a bunch of drawing code
}
The Timeline view in Versions is actually a WebView, the page that you see in it uses javascript to collapse headers you click on.
The rule of thumb I use for where to start out with a custom control is:
To customize the look of a standard Cocoa control:
subclass the appropriate control (like e.g. NSButton and NSButtonCell)
stick as close as makes sense to the way the default control is implemented (e.g. in a buttoncell, start from the existing attributedTitle instance method to draw the button title, unless you always want to draw with the same attributes regardless of what's set up in IB or if you need to draw with different attributes based on state, such as with the trial expiration button in Versions' main window)
Creating an entirely new UI element:
subclass NSView and implement pretty much all mouse and key event handling (within the view, no need to redo "hitTest:") and drawing code yourself.
To present something that's complex, of arbitrary height, but isn't a table:
See if you can do it in HTML, CSS and JS and present it in a WebView.  The web is great at laying out text, so if you can offload that responsibility to your WebView, that can be a huge savings in pain in the neck.
Recommended reading on learning how to draw stuff in your own custom view's drawing methods: Cocoa Drawing Guide
Customizing the look of for instance an NSTableView is an entirely other cup of tea, thanks to the complexity of a tableview, that can happen all over the place.  You'll be implementing your own custom cells for some things you want to do in a table, but will have to change the way rows are highlighted in a subclass of the actual NSTableView object itself.  See for instance the source code for iTableView on Matt Gemmell's site for a clear example of where to draw what.
Finally, I think Abizer's suggestion to go check out the code of BWToolkit is a great idea.  It might be a bit overwhelming at first, but if you can read and understand that code you'll have no trouble implementing your own custom views and controls.
Have a look at some excellent example code: BWToolkit