cocoa binding on NSImageView in a NSTableView's column not update automatically
In my cocoa app there is a NSTableView(10.7 lion view based tableview) only cont开发者_开发问答ains one column, the cell in it is a custom NSTableCellView, in it there are several views and one of them is a NSImageView.
My data model has an integer status variable, I use cocoa binding and a NSValueTransformer to give different NSImage to this NSImageView.
The problem is, the image is not updated automatically. After the status is changed, the image only reflects the change after reload the list.
Thanks for any help :)
Finally I solved my own question.
I added observer for model's "status" integer property. When this status changed, the actual table cell's icon (NSImageView) has changed according to the cocoa binding but not updated. Then in the observe callback method, just add
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
Model *m = (Model*)context;
NSInteger idx = [self.modelArray indexOfObject:m];
NSTableRowView *row = [self.tableView rowViewAtRow:idx makeIfNecessary:NO];
ListCellItem *cell = [row viewAtColumn:0];
cell.icon.needsDisplay = YES;
[cell.icon.image recache];
[cell.icon display];
}
Then the image get updated as soon as status get changed.
精彩评论