{"id":15649251,"url":"https://github.com/konstantinpavlikhin/kspautomaticheightcalculationtablecellview","last_synced_at":"2025-07-14T03:06:12.145Z","repository":{"id":23040863,"uuid":"26393822","full_name":"konstantinpavlikhin/KSPAutomaticHeightCalculationTableCellView","owner":"konstantinpavlikhin","description":"A useful superclass for a custom view-based NSTableView's cell.","archived":false,"fork":false,"pushed_at":"2016-09-05T11:33:13.000Z","size":773,"stargazers_count":40,"open_issues_count":0,"forks_count":8,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-07-11T10:34:50.733Z","etag":null,"topics":["cell","constraints","macos","nstablecellview","nstableview"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/konstantinpavlikhin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-09T11:36:56.000Z","updated_at":"2025-02-23T08:38:15.000Z","dependencies_parsed_at":"2022-08-05T20:15:50.031Z","dependency_job_id":null,"html_url":"https://github.com/konstantinpavlikhin/KSPAutomaticHeightCalculationTableCellView","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/konstantinpavlikhin/KSPAutomaticHeightCalculationTableCellView","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konstantinpavlikhin%2FKSPAutomaticHeightCalculationTableCellView","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konstantinpavlikhin%2FKSPAutomaticHeightCalculationTableCellView/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konstantinpavlikhin%2FKSPAutomaticHeightCalculationTableCellView/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konstantinpavlikhin%2FKSPAutomaticHeightCalculationTableCellView/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/konstantinpavlikhin","download_url":"https://codeload.github.com/konstantinpavlikhin/KSPAutomaticHeightCalculationTableCellView/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konstantinpavlikhin%2FKSPAutomaticHeightCalculationTableCellView/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265236722,"owners_count":23732497,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cell","constraints","macos","nstablecellview","nstableview"],"created_at":"2024-10-03T12:29:01.395Z","updated_at":"2025-07-14T03:06:11.803Z","avatar_url":"https://github.com/konstantinpavlikhin.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"## KSPAutomaticHeightCalculationTableCellView\n\n[![Version](https://img.shields.io/cocoapods/v/KSPAutomaticHeightCalculationTableCellView.svg)](https://cocoapods.org/pods/KSPAutomaticHeightCalculationTableCellView)\n[![Platform](https://img.shields.io/cocoapods/p/KSPAutomaticHeightCalculationTableCellView.svg)](https://cocoapods.org/pods/KSPAutomaticHeightCalculationTableCellView)\n[![License](https://img.shields.io/cocoapods/l/KSPAutomaticHeightCalculationTableCellView.svg)](https://cocoapods.org/pods/KSPAutomaticHeightCalculationTableCellView)\n\nA useful superclass for a custom view-based `NSTableView`’s cell.\n\n## Sample Application\n\nThere is a simple application that demonstrates `KSPAutomaticHeightCalculationTableCellView` in action. The app shows a single window with an embedded split view. The left pane has a fixed-height cells (`AHCUserTableCellView`) that represent some fictional users. The right pane has cells (`AHCMessageTableCellView`) whose heights depend both on their textual content and a column width. When window is resized the table view on the right is notified to re-query the row heights from the delegate.\n\n![](Sample/Screenshot.png)\n\n## How To Use\n\n#### Your custom cell class should inherit from `KSPAutomaticHeightCalculationTableCellView`\n\nThis is how it used to look in Objective-C:\n\n```objective-c\n#import \u003cKSPAutomaticHeightCalculationTableCellView+Private.h\u003e\n\n@interface MyLovelyCell : KSPAutomaticHeightCalculationTableCellView\n…\n@end\n```\n\n#### Put your cell into a separate XIB and let the `KSPAutomaticHeightCalculationTableCellView` know how to find it at runtime\n\nIt is generally a good idea to put your cell into a separate XIB, but usage of a `KSPAutomaticHeightCalculationTableCellView` makes this mandatory.\n\nBy default `KSPAutomaticHeightCalculationTableCellView` will look for a nib file named exactly like your cell's subclass in a main bundle.\n\nThat is, if you call your cell class `MyLovelyCell` you have to name your XIB `MyLovelyCell.xib`. This xib should contain one and only one root view of MyLovelyCell class. You still can have an arbitrary number of other objects in a XIB, though.\n\nYou can customize the behaviour by overriding the following class methods:\n\n```objective-c\n+ (nonnull NSString*) correspondingNibName\n{\n  return @\"SomethingDifferent\";\n}\n```\n\n```objective-c\n+ (nonnull NSBundle*) correspondingBundle\n{\n  return [NSBundle bundle...];\n}\n```\n\n#### Decide whether your custom table cell should have a variable height\n\n**Cell has a variable height** == **Cell's height depends on a represented object value** `OR` **cell's height depends on cell's width**.\n\nBy default, `KSPAutomaticHeightCalculationTableCellView` assumes that your cell has a variable height, since this is the basic reason to use the class in your project at all.\n\nOverride `+hasFixedHeight` and return `YES` if your cell's height is really fixed.\n\n```objective-c\n+ (BOOL) hasFixedHeight\n{\n  return YES;\n}\n```\n\nIn this case `KSPAutomaticHeightCalculationTableCellView` will cache the height after it will be calculated for the first time. Subsequent calls to `+heightWithRepresentedObject:width:` will return the stored value.\n\n#### Layout your cell via Auto Layout\n\nThis step is also mandatory. `KSPAutomaticHeightCalculationTableCellView` only functions in Auto Layout-enabled mode. There is no point of using the class if you perform manual frame computations.\n\n**Attention, this part is really important**:  \nAfter you add layout constraints, the height of your cell __should be unambiguous__! This means, height have to be constraint either directly (via explicit height constraint, which will render `KSPAutomaticHeightCalculationTableCellView` usage pretty pointless, since you already know the height value) or in a derived-fashion (in which height of a cell somehow depends on a geometry of its subviews, and the geometry, in its turn, depends on content).\n\nRemember it, cause if you leave your cell view's height ambigous you will end up with a table view rows having a zero height.\n\n#### Implement a `NSTableViewDelegate` method `-tableView:heightOfRow:`\n\n```objective-c\n- (CGFloat) tableView: (NSTableView*) tableView heightOfRow: (NSInteger) row\n{\n  // Get the width of a column in which the cell should reside.\n  CGFloat columnWidth = [tableView tableColumnWithIdentifier: @“MainColumn”].width;\n\n  // Let the KSPAutomaticHeightCalculationTableCellView to calculate the preffered cell height for you.\n  return [MyLovelyCell heightWithRepresentedObject: #\u003ca model object for this row\u003e width: columnWidth];\n}\n```\n\nAt this point `KSPAutomaticHeightCalculationTableCellView` will create a prototype cell instance, set its `objectValue` property to the passed value, constrain its width to the `columnWidth` and calculate the minimum required height.\n\n#### Make sure you don't make unnecessary work in your table cell view prototype\n\nWhen `KSPAutomaticHeightCalculationTableCellView` instantiates a cell for measurement purposes it sets its boolean `prototype` property to `YES`. If you happen to do some heavy-lifting in your table cell view subclass, for example, doing expensive calculations in response to an `objectValue` change, please refrain from doing so if the calculation result doesn't affect the layout.\n\n```objective-c\nif(!self.prototype)\n{\n  // Do some fancy visual effects pre-calculation (image caching, data detection and so on...).\n}\nelse\n{\n  // This cell is a prototype that is being reused for a row height calculation (possibly hundreds times per second).\n}\n```\n\nThat's it!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkonstantinpavlikhin%2Fkspautomaticheightcalculationtablecellview","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkonstantinpavlikhin%2Fkspautomaticheightcalculationtablecellview","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkonstantinpavlikhin%2Fkspautomaticheightcalculationtablecellview/lists"}