开发者

Use tables as layout container in Cocoa?

First of all; I'm currently employed as webdeveloper, so there are a few things that are burned in my mind, one of them: Don't use tables for layouts, just don't!

I'm also learning Objective-C and working with the Cocoa framework, using a book. Here's a quote from it: "You can use these two methods in combination to turn a table into an arbitrary data list view. Each row can now be any size and drawn using any cell. This lets you draw completely unrelated things in a table view and just use it as a layout container."

It feels just SO wrong to do that... But, is this common 开发者_Python百科in OSX-developing with Cocoa, or should I avoid this?


The motivation behind "don't use tables for layout" is to enforce the separation of presentation (CSS) and content (HTML). That's a markup language thing.

In Cocoa, the paradigm is model-view-controller. You want to keep your model (content) independent of your views (presentation... sorta), but it's not really the same.

If you are talking about iOS, using a UITableView in this way is perfectly kosher. That's because it's job is to manage a table of "cells" which could be made of anything. There is no pretense that it is for a table of information like you might find in a book. The "table" in a UITableView isn't really the same concept as a "table" in HTML; for example, the UITableView can't even support columns in the way a HTML table does.

If you are talking about Mac OS X, the NSTableView is conceptually closer to the HTML table. Most of the time it is used for data, though it could be used to make a grid of checkboxes, for example. (But if that's what you're trying to do, NSMatrix is probably a better fit.)

Note that the worst offenses of using HTML tables for layout involve lots of cells that span multiple columns and rows and/or embedding tables-within-tables, techniques that don't even have analogues in NSTableView or UITableView. You couldn't use an NSTableView to achieve these kinds of layouts.

So:

  • DO NOT obsess about a thing with the word "table" in the name being used for layout. It's not a bad thing, Cocoa is actually very well designed, you just have to get used to the territory.

  • DO stay true to the spirit of keeping content/model separate from presentation/view. It's harder to see at first, since everything is in Objective-C (instead of two separate languages like HTML & CSS).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜