PushViewController 后 self 的值发生变化
我正在使用 UINavigationController,并且想重用 UITableView。为此,我为表属性创建了数据源。这是视图的文件。
WordList.h:
@interface WordList : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>{
}
@property (nonatomic, retain) IBOutlet UIViewController *selfRef;
@property (assign) NSDictionary *wordList;
@property (assign) NSArray *indexList;
@property (assign) UINavigationController *useNC;
- (void)setNavigationController:(UINavigationController *)nc;
- (void)setSource:(NSString *)source;
- (void)dealloc;
@end
WordList.m:
@implementation WordList
@synthesize selfRef;
@synthesize wordList;
@synthesize indexList;
@synthesize useNC;
//static NSDictionary *wordList = nil;
//static NSArray *indexList = nil;
//static UINavigationController *useNC;
- (void)awakeFromNib {
self.title = @"English -> ASL";
}
- (void)viewDidLoad {
if (!wordList){
Database *db = [Database singleton];
if (db) wordList = [db getWordList];
indexList = [wordList allKeys];/*[NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M",
@"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];*/
[wordList retain];
[indexList retain];
}
}
- (void)setSource:(NSString *)source {
Database *db = [Database singleton];
NSLog(@"%@:setSource", self);
if (db) wordList = [db runWordListQuery:source];
NSLog(@"Word list: %@", wordList);
indexList = [wordList allKeys];/*[NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M",
@"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];*/
NSLog(@"Index list: %@", indexList);
[wordList retain];
[indexList retain];
}
- (void)setNavigationController:(UINavigationController *)nc {
NSLog(@"%@:setNavigationController(%@)", self, nc);
useNC = nc;
}
- (void)dealloc {
NSLog(@"WordList:dealloc");
if (wordList) [wordList release];
if (indexList) [indexList release];
[super dealloc];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
EntryViewer *ev = [[EntryViewer alloc] initWithNibName:@"EntryViewer" bundle:nil];
if (ev){
[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
[ev setEntry:[[[wordList objectForKey:[indexList objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] ident]];
if (useNC){
[useNC pushViewController:ev animated:YES];
}
else {
[APP_LISTNAV pushViewController:ev animated:YES];
}
[ev release];
}
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return indexList;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [indexList objectAtIndex:section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"%@:numberOfSectionInTableView(%u): %@", self, [indexList count], indexList);
return [indexList count];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
NSInteger section = -1;
section = [indexList indexOfObject:title];
return section;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [[wordList objectForKey:[indexList objectAtIndex:section]] count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [[[wordList objectForKey:[indexList objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] description];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = CellIdentifier;
return cell;
}
@end
我仍然在 Objective-C 属性方面摸索,所以我可能会误用它们。问题是,即使 setSource: 中的分配有效,该表也没有显示任何数据。请参阅下面的 NSLog() 输出。
2011-10-17 10:58:22.387 ASL Dictionary[381:207] <WordList: 0x614f0b0> - WorldList:setSource
2011-10-17 10:58:22.394 ASL Dictionary[381:207] Word list: {
H = (
"HOME (noun)"
);
}
2011-10-17 10:58:22.395 ASL Dictionary[381:207] Index list: (
H
)
2011-10-17 10:58:22.396 ASL Dictionary[381:207] <WordList: 0x614f0b0> - WorldList:setNavigationController(<UINavigationController: 0x573e780>)
2011-10-17 10:58:22.402 ASL Dictionary[381:207] <WordList: 0x57806f0> - numberOfSectionInTableView(0): (null)
很明显为什么表格没有显示数据,调用 numberOfSectionsInTable:
返回 0。我想知道的是为什么。正如您从日志中看到的那样,我非常确定这是因为 self
的值与前两次调用不同。这是为什么呢?第三次调用是在调用 pushViewController:
之后发生的。是这个原因吗?我是否这样做错了,或者我错过了一些简单的事情?
感谢您抽出时间。
I'm using a UINavigationController and I want to reuse a UITableView. To that end, I've made the data sources for the table properties. Here are the files for the view.
WordList.h:
@interface WordList : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>{
}
@property (nonatomic, retain) IBOutlet UIViewController *selfRef;
@property (assign) NSDictionary *wordList;
@property (assign) NSArray *indexList;
@property (assign) UINavigationController *useNC;
- (void)setNavigationController:(UINavigationController *)nc;
- (void)setSource:(NSString *)source;
- (void)dealloc;
@end
WordList.m:
@implementation WordList
@synthesize selfRef;
@synthesize wordList;
@synthesize indexList;
@synthesize useNC;
//static NSDictionary *wordList = nil;
//static NSArray *indexList = nil;
//static UINavigationController *useNC;
- (void)awakeFromNib {
self.title = @"English -> ASL";
}
- (void)viewDidLoad {
if (!wordList){
Database *db = [Database singleton];
if (db) wordList = [db getWordList];
indexList = [wordList allKeys];/*[NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M",
@"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];*/
[wordList retain];
[indexList retain];
}
}
- (void)setSource:(NSString *)source {
Database *db = [Database singleton];
NSLog(@"%@:setSource", self);
if (db) wordList = [db runWordListQuery:source];
NSLog(@"Word list: %@", wordList);
indexList = [wordList allKeys];/*[NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M",
@"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];*/
NSLog(@"Index list: %@", indexList);
[wordList retain];
[indexList retain];
}
- (void)setNavigationController:(UINavigationController *)nc {
NSLog(@"%@:setNavigationController(%@)", self, nc);
useNC = nc;
}
- (void)dealloc {
NSLog(@"WordList:dealloc");
if (wordList) [wordList release];
if (indexList) [indexList release];
[super dealloc];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
EntryViewer *ev = [[EntryViewer alloc] initWithNibName:@"EntryViewer" bundle:nil];
if (ev){
[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
[ev setEntry:[[[wordList objectForKey:[indexList objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] ident]];
if (useNC){
[useNC pushViewController:ev animated:YES];
}
else {
[APP_LISTNAV pushViewController:ev animated:YES];
}
[ev release];
}
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return indexList;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [indexList objectAtIndex:section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"%@:numberOfSectionInTableView(%u): %@", self, [indexList count], indexList);
return [indexList count];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
NSInteger section = -1;
section = [indexList indexOfObject:title];
return section;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [[wordList objectForKey:[indexList objectAtIndex:section]] count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [[[wordList objectForKey:[indexList objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] description];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = CellIdentifier;
return cell;
}
@end
I'm still finding my feet in regards to Objective-C properties so I may be misusing them. The problem is that, even though the assignments in setSource: work, the table shows no data. Please see the NSLog() output below.
2011-10-17 10:58:22.387 ASL Dictionary[381:207] <WordList: 0x614f0b0> - WorldList:setSource
2011-10-17 10:58:22.394 ASL Dictionary[381:207] Word list: {
H = (
"HOME (noun)"
);
}
2011-10-17 10:58:22.395 ASL Dictionary[381:207] Index list: (
H
)
2011-10-17 10:58:22.396 ASL Dictionary[381:207] <WordList: 0x614f0b0> - WorldList:setNavigationController(<UINavigationController: 0x573e780>)
2011-10-17 10:58:22.402 ASL Dictionary[381:207] <WordList: 0x57806f0> - numberOfSectionInTableView(0): (null)
It's clear why the table shows no data, the call to numberOfSectionsInTable:
returns 0. What I want to know is why. I'm pretty certain, as you can see from the log, that it's because the value of self
is different from the previous two calls. Why is this? The third call comes after the call to pushViewController:
. Is that the reason? Am I approaching this wrong or have I missed something simple?
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的节数返回
nul
,这可能是问题所在。尝试将其硬编码为 1(
return 1;
),同时 NSLogCellIdentifier
的值,因为您可能不会返回任何内容。华泰
Your number of sections is returning
nul
, which could be the problem.Try hard-coding it to 1 (
return 1;
) also NSLog the value ofCellIdentifier
as you may be returning nothing there.HTH