The correct way to user custom UITableViewCells - objective-c

I have seen a lot of different ways of implementing custom cells in a table view.
Like different file owners, get it from bundle and call the the latest obj of the array and a lot more.
But all did not feel right.
What is the best and correct way to create and use custom table view cells (with interface builder).

I think Storyboards are the new proper way. I use this method:
http://iphonedevelopment.blogspot.com/2009/09/table-view-cells-in-interface-builder.html
And it works quite well. I think it's somewhat proper in that you are asking the OS to do most of the work, although it's a little sneaky that the cell is assigned to a property as part of the NIB loading as a side effect.

Had the same problem. For me it is now solved with storyboards in ios5.

Related

Objective-C: Creating a file purely for holding variables

I would like to add a file to my project, who's sole purpose would be to hold an array. I would then #import this file wherever I need to add/get from the array.
My problem is that when I create a new file (I'm using Xcode 4), I'm not sure what type of template to choose, and then what subclass to choose.
My reason for doing all of this is because I have a SplitView-Based app, and one of the views has a textfield, where I am trying to output data. My problem is that whenever I switch to a different view and then switch back, only the most recent entry is there. I am not 100% why that is but I suspect it is because when I switch to a different view, the current view is forgotten about, along with the variables in it.
This is not a good way to do it. There are many ways to do what you want: prepareForSegue: if you are using storyboards, delegation, instantiating your viewcontroller in code and setting a property in the header-file..those are just a few ways.
The way you are proposing is a slippery slope to bad Objective-C code and is only going to cause you more headaches in the future. Take the time to learn to do it right.
Check out this to get you thinking in the right direction.
How you save your data doesn't appear to be your problem. Take a look at the MVC design pattern and how view controllers implement it. They often rely on a dataSource protocol, which links the data from a "Model" to your "View" in a logical way to achieve your intended purpose.
The view controller should then be able to assign a delegate (usually itself (self) to keep the view populated with the correct data, whether the view gets unloaded or not.
If your view controller doesn't refer to a data source or a corresponding protocol, it would still be worth your time to see how you might take advantage of that design pattern. It will pay off in the long run to know this.
Instead of saving variables to a text file, you should consider using NSUserdefaults instead.
But I don't think that's the real solution to your problem, just wanted you know that there are other ways than saving stuff to a text file.

Customized UITableView

I just want to confirm that which is the better way for handling the highly customized table view, is it by using the nib file or writing the code for each element ourself.
This question has been asked not only in regard to UITableViews. You can see some tests here:
http://cocoawithlove.com/2010/03/load-from-nib-or-construct-views-in.html
If the cells are very complex, it is easier to create a nib file for the cell, like here
http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/
Memory wise, I can't tell you for sure.
But considering you are talking about a highly customized table view, using nibs is better because you can actually see the modifications. This makes it easier for you. Otherwise it would be a real pain to create a highly customized cell only by code, not to mention that it would make it nearly impossible to be edited by someone else than a programmer.
The best way will be to create a separate nib only for your cell layout as described here: UITableViewController with custom UITableViewCell

How to structure a very large Objective-C class?

I've a fairly complex window that is backed by a controller class that is obviously growing to meet the needs of my view window. While I believe I am sticking to proper MVC I'm still having problems managing a fairly largish controller class.
How do you breakdown your objects? Maybe use Categories? For example, one category to handle the bottom part of the window, another category to handle my NSOutlineView, another category to handle a table, and so on and so forth?
Any ideas or suggestions are welcome.
It sounds like it's a complex window controller that's growing to unmanageable proportions? This is getting to be a more common issue because of applications which, like the iApps, do most of their work in a single window.
As of Leopard, the recommended way of breaking it down is to factor out each part of the window into its own NSViewController subclass. So, for example, you'd have a view controller for your outline view, and a view controller for each of your content views, etc.
Also, I'd like to second the use of #pragma marks to divide code files up into segments, and in addition to categories, I also like to use class extensions for private methods.
It's a simple answer, but the code folding feature of the Xcode IDE can be handy for focusing your attention on sections of a class. Another little thing that might help is going to View->Code Folding and turning on Focus Follows Selection. This makes it so the background color of the scope of your current selection is white while everything else is shades of gray.
Categories are ideal for this. Create a new file for each category, and group them by functionality, as you suggested.
I've tried using Categories in situations like this and I just end up confusing myself, wondering how in the world I'm calling that method when it's "obviously" not in the class I'm looking at.
I'd recommend liberal use of #pragma mark in your source code. Makes it super-easy to browse through all your methods.

Can't make empty selection in NSTableView

I don't know how it happened, but all of a sudden in my table view I can't make an empty selection anymore. Like a table view row always has to be selected, and it can't be deselected by clicking somewhere else in the table view. I can select a different row, but I can't make an empty selection.
In the Interface Builder attributes for the table view empty selection is enabled, so I don't know where to look next. The one major change I made was that I installed OS X Snow Leopard. I'm not sure if this issue has something to do with that.
Thanks
I struck this exact same issue (I am using XCode 4.2 but compiling against the 10.6sdk). NSOutlineView::deselectAll just was not deselecting things. I have a fairly complex NSOutlineView which exhibits the same behavior. I had a look on the apple developer forums and other places to try and work around this issue. However in the end working around this for me was very simple and I could just use:
- (void) myDeselectAll
{
[self selectRowIndexes:[NSIndexSet indexSet] byExtendingSelection:NO];
}
Try doing it programmatically with the setter method setAllowsEmptySelection:. Alternatively, try disabling empty selection in IB, saving, then enabling it, saving one more time. That might fix it.
Also make sure that something in tableView:shouldSelectRow: isn't stopping you from it (provided you've implemented this delegate method).
This is a bit old, but for those who need an answer to this:
Use the interface builder and mark the array controller. remove checkmark "avoid empty selection".
If not done so, create an outlet for the array controller. Here I have called it DocumentArrayController.
then to empty selection:
[_DocumentArrayController removeSelectionIndexes:
[_DocumentArrayController selectionIndexes]];
Do you have your columns bound to an array controller? If so, check the controller's attributes.
I'm not allowed to say much more than this: It seems to be a problem with 10.6 specifically

Displaying a list of views

Here's a mockup:
A mockup http://img.skitch.com/20090228-mqdj17xijycc98spf181a8q6q7.jpg
I have been trying to find some sample code that showcases something like this -- a scroll view with a list of custom views. Haven't found anything. I've been trying to find some open source projects that does this (that isn't Adium with a million files and lines of code), but haven't found anything there either.
I've been told that I can use NSMatrix to achieve this. Again, haven't found any sample code.
Anyone got some suggestions? Or sample code ; )
You could also use NSTableView and create a custom subclass of NSCell to render the content.
If you can require Leopard, take a look at NSCollectionView. The API is a little weird but it's pretty powerful once you get the hang of it.
I've been told that I can use NSMatrix to achieve this.
You can't. NSMatrix uses cells, not views.
To answer your question: What James Williams said.