Rounak
August 1, 2010, 2:41pm
#1
#import <Cocoa/Cocoa.h>
@interface Controller: NSObject {
int theInt;
NSTableView *tableView;
}
@property (readwrite, assign) int theInt;
(IBAction) changetheInt: (id) sender ;
@end
#import <Foundation/Foundation.h>
#import “Controller.h”
@implementation Controller
@synthesize theInt;
(id)init
{
[super init];
[self setValue:[NSNumber numberWithInt:5]
forKey:@“theInt”];
NSNumber *n = [self valueForKey:@“theInt”];
NSLog(@“theInt = %@”, n);
return self;
}
(IBAction) changetheInt: (id) sender {
[self willChangeValueForKey: @“theInt”];
theInt++;
NSLog(@“theInt is now %d”, theInt);
[self didChangeValueForKey: @“theInt”];
[tableView reloadData];
}
(int)numberOfRowsInTableView:(NSTableView *)aTableView {
return 1;
}
(id)tableView:(NSTableView *) tv
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row
{
return [self valueForKey: @“theInt”] ;
}
@end
IB Elements:
One tableView
One button
IB Connections:
table View x Controller delegate:
table View x Controller datasource:
Button x Controller changetheInt:
Problem:
When button is pressed, the value of theInt changes as I observe it in Console but it does not change in tableView.
Only when I click on the row of tableView, the value is updated. As you can see above i am already using tableView reloadData.
Thanks.
ief2
August 1, 2010, 3:07pm
#2
If you subclass a class, even if it is NSObject, you have to assign self to the super-call, not just call [super init] , this will generate a memory leak.
(id)init
{
self = [super init];
[self setValue:[NSNumber numberWithInt:5]
forKey:@“theInt”];
NSNumber *n = [self valueForKey:@“theInt”];
NSLog(@“theInt = %@”, n);
return self;
}
I also don’t understand why you are using setValue:ForKey: . Isn’t it easier to just use the instance variable?
#import <Cocoa/Cocoa.h>
@interface Controller: NSObject {
int theInt;
IBOutlet NSTableView *tableView;
}
@property (readwrite, assign) int theInt;
(IBAction)changetheInt:(id)sender;
@end
#import “Controller.h”
@implementation Controller
@synthesize theInt;
(id)init {
self = [super init];
if(self != nil) {
theInt = 5;
NSLog(@“theInt = %d”, theInt);
}
return self;
}
(IBAction)changetheInt:(id)sender {
NSLog(@“theInt is now %d”, ++theInt);
[tableView reloadData];
}
(int)numberOfRowsInTableView:(NSTableView *)aTableView {
return 1;
}
(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
return [NSString stringWithFormat:@“%d”, theInt];
}
@end
Hope it helps,
ief2
Rounak
August 1, 2010, 4:04pm
#3
Thanks for the suggestions.
However, it does not solve the problem. There is no change in the result.
Try calling needsDisplay.
Rounak
August 2, 2010, 1:21am
#5
(IBAction)changetheInt:(id)sender {
NSLog(@“theInt is now %d”, ++theInt);
[tableView reloadData];
[tableView setNeedsDisplay: YES];
}
fails.
only binding works as desired.
I’m not sure you should be changing the ivar directly, but if you do, you’ll have to include willChangeValueForKey and didChangeValueForKey.
ief2
August 2, 2010, 10:23am
#7
Are you sure you’ve set the data source and IBOutlet thru IB? The code in this project works fine for me:
http://www.mediafire.com/?56ma4y1347s41e0
Hope it helps,
ief2
Rounak
August 5, 2010, 5:48am
#8
Sorry for the delay in replying.
I had mentioned the above connections in my first post.
Instead of
table View x Controller delegate:
the connection
table View x Controller
was required.
Thanks for the help.