pushViewController之后自我更改的值

时间:2011-10-17 15:24:49

标签: objective-c uiviewcontroller uinavigationcontroller 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:之后发出的。这是什么原因?我接近这个错误还是我错过了一些简单的东西?

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

您的部分数量正在返回nul,这可能是问题所在。

尝试将其硬编码为1(return 1;),同时NSLog为CellIdentifier的值,因为您可能不会在那里返回任何内容。

HTH