Я использую 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: work, таблица не показывает данных. Пожалуйста, смотрите вывод 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:
. Это причина? Я подхожу к этому неправильно или я пропустил что-то простое?
Спасибо за ваше время.