Question

I'm new to Objective C, and to the iPhone and Mac development environments in general. I like the object model - it takes me back about 20 years to when I first started OO programming in the way that the object methodologies are implemented. Much of the basics seem to have been lost in the ensuing years, and to some extent, this is a much purer environment.

Learning the language and xCode is another story altogether though, and I don't yet have the hand of IB, and to be honest, I don't find it all that intuitive.

I'm in the process of creating an iPhone app, and I need to display data in a tableview. For the reasons noted above, I'm not using Interface Builder, and, so far I've created pretty much everything else by hand. For me, that's by far the best way of learning and understanding how everything hangs together.

But so far I've not seen very much in the way of examples of how to handcraft a UITableView object to place onto a view. Nothing usable, in any case. There's some discussion items within the Apple documentation, but it's not what I would anything like a practical working example.

Does anyone have any such examples, please?

Thanks in advance for any and all contributions

Was it helpful?

Solution

To create your own UITableView just instantiate the class, and add it to your viewController's view. (IB just does this for you). It's the same with any built in object that's a sub class of UIView.

UITableView *tableView = [[UTableView alloc]init];
[[self view] addSubview:tableView];

There's a lot of other things you'll need to do like set the frame property so it fills the view you're adding it too, and set the style and everything else.

Best bet is to read up on the Table View Programming Guide then look through the UITableView Class Reference

side note: You should really just use IB when you can. It saves a lot of headaches and time, and once you realize it's just saving you lines of code you have to maintain you will enjoy it!

OTHER TIPS

You are swimming upstream. Learn to use Interface Builder to make your life easier. Take a look at this tutorial and you should start to understand it a bit better:

iPhone Application Example

Also, I highly recommend Erica Sadun's iPhone Developer's Cookbook. The second edition should be available in December. Just buy it. You won't be sorry.

For an example of an application that creates all of its views programmatically, I can direct you to the source code of my application Molecules. It makes very little sense to manually create UITableViews, because you'll want to manage them via UITableViewControllers.

As Matt points out, using Interface Builder is probably the preferable way of doing this now. I originally wrote this application when Interface Builder was less capable than it is today. However, I have seen a very slight reduction in application startup time when using a view hierarchy entirely generated in code vs. one deserialized from a NIB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top