表视图不根据绑定更新 [英] Table view not updating according to bindings

查看:166
本文介绍了表视图不根据绑定更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常新手的问题,这是我之前做过很多次,但有一些我错过的时间。

This is a very newbie question, and this is something I have done many times before, but there's something I'm missing this time.

在我的AppDelegate。 h文件我声明一个NSArray并将其设置为属性:

In my AppDelegate.h file I declare an NSArray and set it as a property:

@interface AppDelegate : NSObject {
NSArray *lines;

}

@property(readwrite, retain) NSArray *lines;
@end

然后在awakeFromNib方法中的AppDelegate.m文件中:

And then in the AppDelegate.m file in the awakeFromNib method I alloc it:

lines = [[NSArray alloc] init];

然后我有一个方法来设置lines数组:

Then I have a method that sets the lines array:

NSString *fileContents = [NSString stringWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/sometextfile.txt"] encoding:NSUTF8StringEncoding error:NULL];
lines = [fileContents componentsSeparatedByString:@"\n"];



我有一个数组控制器绑定到 AppDelegate.self.lines 然后我有一个表列绑定到 Array Controller.arrangedObjects 。我可以确认数组正在更新(使用NSLog测试),但是表的内容没有更新(它仍然是空白的)。

I have an array controller thats bound to AppDelegate.self.lines then I have a table column bound to Array Controller.arrangedObjects. I can confirm that the array is being updated (tested using NSLog) however the contents of the table are not being update (it remains blank).

有什么明显的'm missing here?

Is there something obvious I'm missing here?

推荐答案

如果使用绑定,则不需要数据源。一个或另一个。

You don't need a data source if you're using Bindings. One or the other.


我有一个数组控制器绑定到AppDelegate.self.lines...

I have an array controller thats bound to "AppDelegate.self.lines" …

为什么 self


@property(readwrite,retain)NSArray *行;

@property(readwrite, retain) NSArray *lines;

否,使用 code>这里。否则,你会发现自己保留了其他人的mutable数组,然后他们会改变。

No, use copy here. Otherwise, you'll find yourself retaining someone else's mutable array, which they will then mutate. Then "your" array will have changed without you knowing it.


然后我有一个方法来设置lines数组:

Then I have a method that sets the lines array:

lines = [fileContents componentsSeparatedByString:@"\n"];


这就是为什么表不显示任何内容。你不是通过属性,你直接访问实例变量。直接的实例变量访问不会导致KVO通知,所以数组控制器从来不会发现变化。

This is why the table doesn't show anything. You're not going through the property, you're accessing the instance variable directly. Direct instance variable accesses do not cause KVO notifications, so the array controller never finds out about the change.

更糟糕的是,你泄漏了旧的数组分配而不释放它)和欠保留这个新数组。因为你不保留新数组,那个实例变量很快就会拥有一个死对象。自动保留由 setLines:方法完成,只有在调用它时才会调用。

Even worse, you're leaking the old array (since you simply assign over it without releasing it) and under-retaining this new array. Because you're not retaining the new array, that instance variable will hold a dead object shortly. The automatic retaining is done by the setLines: method, which only gets called when you call it.

通过属性:

self.lines = [fileContents componentsSeparatedByString:@"\n"];

属性访问是一个隐式访问器消息,因此这既保留了数组您按照上述建议更正了 @property )并发布了KVO通知。

A property access is an implicit accessor message, so this both retains the array (or copies it, once you correct the @property as I suggested above) and posts KVO notifications.

这篇关于表视图不根据绑定更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆