Re: Making custom table cells for view-based tables on mac?
Re: Making custom table cells for view-based tables on mac?
- Subject: Re: Making custom table cells for view-based tables on mac?
- From: Quincey Morris <email@hidden>
- Date: Mon, 23 Nov 2015 22:08:36 -0800
- Feedback-id: 167118m:167118agrif8a:167118szB1wZZYaK:SMTPCORP
On Nov 23, 2015, at 16:06 , Alex Hall <email@hidden> wrote:
>
> When I look at the cell I got by default, it has a lot of levels to it. I'm not sure where to add my own views, or how to assign a separate view controller to the cell (just for modularity's sake). What I see is this:
>
> Tweet cell contains: table cell view, text cell..
> Tweet Cell -> table cell view contains: table view cell
> table view cell contains: table view cell (yes, a second one)
>
> I believe I'd add my views to the top level, so they are on the same level as the text cell, but I'm not sure.
You’ve described three levels here, but there are actually four.
The first (top) level is actually the table column. Inside it is a table cell view and a NSCell that you should ignore.
The second level is the table cell view. It contains a text field.
The third level is the text field. Inside it is a NSCell that you should ignore.
So you’ll add views into the second level, that is as siblings of the text field at level 3, but you’ll have to do some preparation first.
> The docs say I should have a label and an image for free, but I don't see either one here.
In OS X, you drag in “Image & Text Table Cell View” from the object library, as a sibling of the table cell view at level 2. Then delete the original table cell view. (I think this is different for UITableView.)
> Furthermore, I want my image on the right, not the left, unless there's a HIG reason not to do that? Even if the thumbnails end up being on the left, I want to understand how to change that if I ever wanted to.
The image is going to appear on the left (or perhaps on the right for right-to-left locales). I’m not sure if you can fiddle with this in IB — the eventual placement might be the result of layout decisions in the NSTableCellView class.
If IB won’t let you put the image where you want it, you can always go back to the text only cell, and add your own custom image subview.
> Once I get my views in place, I'll have to hook them to the view controller, correct? Normally I'd not even ask this, but I had to hook up nothing, and my cell already has a label that's not an outlet, so I'm not sure if special rules apply to cells.
Nope. If you have custom subviews, you need to subclass NSTableCellView to provide outlets to your new subviews, and potentially other behavior. (The standard NSTableCellView may have some programmatic auto-layout behavior involving the two standard subviews.)
If your custom subviews have bindings, you might not need the outlets, but I don’t see any harm in having the outlets anyway. Once you change the cell view subclass from NSTableCellView to your custom subclass, you can drag your other subviews into (i.e. inside) the 2nd level of Tweet cell, hook up the outlets in IB, and add bindings and autolayout constraints.
The last thing you need to do, if you aren’t using bindings, is update your tableView:viewForTableColumn:row: delegate method to set custom subview values when configuring the cell view for a particular row/column.
Fun stuff. Subclassing NSTextCellView isn’t something I do very often, so be aware I’ve probably got a few details wrong.
> Can I have a separate view controller for my cell, just to keep things organized? I tried to have one for a scroll area inside my main scene, but Xcode wouldn't let me. It seems I can have one controller per scene?
Nope. Cell views aren’t managed by controllers. The tableView:viewForTableColumn:row: delegate method is basically the controller.
> According to one doc I was reading, VoiceOver automatically speaks the contents of a cell's text field. What if there are multiple ones, though? Or if I want VO to ignore the image cell but speak other cells in the row? Or speak something different--can I add an accessibility description to a whole cell to have VO speak that? If so, it'll be interesting to see if I have to interact with the table first, or if not interacting still reads the description instead of the text.
Any subviews you add are just views, so they can have the necessary accessibility information to describe them individually. I’d assume you can just do it at the table cell view level, if you want, but you might have to remove standard accessibility annotations on the standard text and image subviews.
> Am I confusing multiple fields per cell with multiple cells? I know I can have columns in a Mac table, unlike on iOS, but I don't really want that, I don't think. If I use multiple columns, once a VoiceOver user interacts with the table, up/down move by cell, not by row. Thinking about it, that might be good in some situations… Visually, is there value to having multiple columns versus multiple text labels in a single column? Would the answer change if I decided to add a button to each table row, to open a popover with additional details about the tweet? That is, would be button have to be its own cell, or could I still add it to a single cell and have it look okay?
No, you’re not confusing subviews with columns. If your subviews are laid out horizontally with fixed spacing, there’s not going to be a lot of difference in appearance. The point of a single column is that you have more flexibility in placing the multiple subviews. However, customizing the table cell view with new custom subviews is a bit awkward. If you’re prepared to accept a more rigid format, multiple columns might be easier. (For example, you could get the image on the right pretty easily, by putting it in a 2nd column.)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden