Andrew Wagnerdrewag.me
← All posts
Andrew Wagner1 min read

Bind a UITableView to a Property

A library to allow binding a UITableView directly to a to-many property. The library automatically handles insertions and deletions as long as you make sure the to-many property emits KVO notifications.

Bind a UITableView to a Property

A couple weeks ago I wrote a post Objective-C Bindings about a library I started that allows you to bind the property of one object to the property of another. This week I added a new type of property binding that allows you to bind a UITableView directly to a to-many property.

You can find the code on github.

Usage#

The binding will set itself as the data source for the table view and an insertion, deletion, or replacement KVO event is triggered on the observed property the binding will automatically add, remove, or update the appropriate cells.

The binding also takes a block that should convert an object from the to-many property into a cell. This is called every time the table view asks for a cell from its data source.

The binding is available in a UITableView extension. Connecting the binding looks like the following:

#import <PropertyBindings/PropertyBindings.h>
 
// ...
 
[self.tableView
    bindToObserved:sourceObject
    withArrayKeyPath:@"names"
    cellCreationBlock:^UITableViewCell *(NSString *name) {
        UITableViewCell *cell = [self.tableView
        dequeueReusableCellWithIdentifier:CellIdentifier];
 
        if (!cell) {
            cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier
            ];
            [cell autorelease];
        }
 
        cell.textLabel.text = name;
 
        return cell;
    }
];

Improvements#

Array properties do not by default trigger the granular insertion, deletion, and replacement notifications required for the binding to work. It would be better if the notifications were handled automatically by the binding.

The first step would be to handle this for arrays. One possible solution is to replace the array with a proxy object that would trigger the notifications. However the setter for the property would have to be swizzled to ensure the proxy stays in place when the property is overwritten.

Subscribe

New posts on company building, working with AI, and shipping software — straight to your inbox. No spam, unsubscribe anytime.

Or follow viaRSSXBlueskyLinkedIn

5 Core iOS Competencies to Be a Great Hire

A list of core skills that are necessary in almost every app. If you can do all of these things reliably, you are well on your way to being a great hire as an iOS developer

15 min read