0

I'm working on an MacOS/Cocoa app where an NSTableView contains information on multiple items where information on each item is displayed on multiple rows in a multi-column table with a table-wide "title" row displayed above the information for each item's data.

Here I have three columns: "Page", "Term" and "Popularity". I've implemented code that sets the formatting of the cells in the title row to black with white text via tableView(:viewFor:row:) and have also created a custom row object (the light blue) via custom NSTableRowView::draw( dirtyRect: NSRect).

However, I also need to have the title (truncated to "Her" in this case) span the entire table width and have not found any solutions posted for something like this. Basically I just need the view for the black row w/white text go all the way across all three columns.

Sample of Failing milti-column row

Researching the problem I've found a number of potential leads for possible solutions but none seem like a clear winner:

  1. Use of the custom NSTableViewRow and a hand-added NSTextField that floats above the cells in the table, though I've not tried it yet and no idea if the scrolling/stacking will be a headache. Not even sure how to implement something like that but I imagine I could figure it out.

  2. Apple documentation refers to NSTableViewRow::var isGroupRowStyle but I cannot find for the life of me any documentation that actually explains what that's for / how it's used so no idea if that would be of help.

  3. The use of NSTableView "Sections" was also recommended and looks very promising. However it looks to be iOS specific (could be wrong here) and I've not found anything like an example that shows it working w/multiple columns.

Basically what I'm needing is something simple like the HTML RowSpan feature but that's probably expecting too much. Any suggestions for best solutions for this and/or coding examples I could look at?

Jc Nolan
  • 450
  • 4
  • 15
  • Is this what you are looking for? https://stackoverflow.com/questions/13769225/view-based-nstableview-with-sections – Marek H Sep 08 '21 at 17:23
  • Thanks @MarekH, that was actually the first thing I found. However like every other solution I found it seems to be single column... I could possibly create a custom nib-based view (which I've no idea how to do / not done before) based on the solutions given but am hoping for a solution which does not have a several days long learning curve – Jc Nolan Sep 08 '21 at 17:48
  • Implement `- (BOOL)tableView:isGroupRow:` in your table delegate and return `YES` for the header row(s), and `NO` for regular (multi-cell) rows. When you do this, the request for the cell for that row will expect a single cell that spans the entire width of the table—it doesn't matter how many columns you have. You know this because the table will pass `nil` for the `NSTableColumn` identifier parameter for row cells. – James Bucanek Sep 08 '21 at 18:18
  • You are only telling what you want to know. There isn't a single line of code. We don't know if you are using cell-based or view-based table view. We don't know whether or not you are using an `NSTableCellView` object. So what do you mean by best solutions? How can one solution the best of all? Who decides how? – El Tomato Sep 08 '21 at 21:50
  • Thanks @JamesBucanek, I think that's what I was looking for. I've almost got it working and will post code tomorrow. Only odd problem is that the AttributedString value for the NSTableCellView is rejecting all formatting otherwise the solution is just what I was hoping for. But my code base is complex (1000s of lines just in this section) so very possible something is stomping on something. I'll try it with a base case and check back tomorrow – Jc Nolan Sep 08 '21 at 23:27
  • Thanks again @JamesBucanek. I created a sample app and unfortunately am running into the same problem there with your solution as I am in my main codebase in that the solution works but NSAttrinutedStrings do not function as expected with isGroupRow. I'm thinking to post a separate posting on that here but in the mean time if I can't solve the problem I'll likely have to move to a custom-view based approach. Here's the sample app in the mean time if anyone has the time to take a look https://github.com/jcnolan/NSTableViewIsGroupRowTest – Jc Nolan Sep 09 '21 at 21:33
  • If you are supplying a view for the header row and it draws across the entire row, then `isGroupRow` is doing it's thing. If you have other formatting or layout issues, that's a different problem. HTH – James Bucanek Sep 12 '21 at 17:33
  • Thanks @JamesBucanek - still working with it. The sample app I created has exactly the same problem. I'm guessing that Swift is using a canned formatting for group rows that somehow needs to be overwritten somehow? I posted a follow up question for that issue here if you are able to take a look https://stackoverflow.com/questions/69154227/formatting-for-nsattributedstring-disappears-in-nstablecellview-nstableview-wh – Jc Nolan Sep 12 '21 at 18:38

0 Answers0