TableView委托消息未被调用

时间:2013-05-18 20:42:51

标签: objective-c uitableview

解决:不是真正的解决方案,但我只使用.xib而不是尝试使用故事板。无论我试图用故事板做什么,都会以某种方式搞砸了被调用的TableView委托方法。感谢你们提供的所有见解。

我有一个UIViewController,它包含一个应该由数组填充的UITableView。 不幸的是,UITableView没有被数组填充(非空)。

我的.h文件:

@interface Directory : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> {
NSArray *listData;
NSArray *savedListData;
MKMapView* mapUI;
UISearchBar* searchBar;
NSArray * originalListData;
UITableView* placeList;
DirInfoListing *infoInst;
NSString *searchInit;
NSString *openInit;
NSMutableArray *buildings;}
@property (nonatomic, retain) NSArray *listData;
@property (nonatomic, retain) NSArray *savedListData;
@property (nonatomic, retain) IBOutlet UISearchBar* searchBar;
@property (nonatomic, retain) IBOutlet UITableView* placeList;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil list:    (NSArray*)array  mapView:(MKMapView*) map;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil list:(NSArray*)array  mapView:(MKMapView*) map search:(NSString*) ss;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil list: (NSArray*)array  mapView:(MKMapView*) map open:(NSString*) openTx;
-(void)doDBLoad;
@end

我的.m文件中的相关内容:

#import "Directory.h"

@interface Directory ()

@end

@implementation Directory
@synthesize listData, savedListData;
@synthesize searchBar;
@synthesize placeList;

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1; 
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"ListData count: %lu", (unsigned long)savedListData.count);
        //This returns a valid number of 74 items
    return [self.savedListData count];
}

/*
 * Populates the table with list data and provides title. AND DOES NOT GET CALLED
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *tID = @"tablIDDirect";
    UITableViewCell *c = [tableView dequeueReusableCellWithIdentifier:tID];
     if(c == nil) {
        c = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tID];
    }

    NSInteger r = [indexPath row];
    if([self.listData objectAtIndex:r] != nil){
        c.textLabel.text = ((DBBuilding*)[self.savedListData objectAtIndex:r]).Name;
        c.detailTextLabel.text = ((DBBuilding*)[self.savedListData objectAtIndex:r]).Acronym;
    }
    c.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return c;
}

/*
 * Displays an entry according to the table selection and redirects the user to the     Directory Information Listing.
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger r = [indexPath row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if(infoInst != nil) {
        infoInst = nil;
    }
    infoInst = [[DirInfoListing alloc] initWithNibName:@"DirInfoListing" bundle:nil building:[self.savedListData objectAtIndex:r] map:mapUI];
}

/*
 * Notification that the Directory is the active view
 */

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if(openInit != nil)
    {
        [self openEntry:openInit];
        openInit = nil;
    }

    placeList = [[UITableView alloc] init];

    placeList.dataSource = self;
    placeList.delegate = self;
    [placeList reloadData];
}

/*
 * Notification that the Directory is no longer the active view
 */

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)viewDidLoad
{
    NSLog(@"ListData count: %lu", (unsigned long)listData.count);
        //Also returns a valid number of 74 objects within the array
    savedListData = [[NSArray alloc] initWithArray:listData];
        //That get put into another array just for my sanity

        [super viewDidLoad];
    // Do any additional setup after loading the view.
    searchBar.text = searchInit;
    [self performSearch:searchInit];    
 }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

调用-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法,但没有调用其他UITableView委托方法,我不知道为什么会发生这种情况。

此外,使用.xibs从我的另一个应用程序(可以正常工作)中提取这一大块代码,并且不使用故事板。这个应用程序(我遇到问题)使用的故事板,我不是很熟悉。我可能在调用和使用故事板方面做错了,但我不确定。如果可能存在相关内容,我可以提供更多信息。

请你们查看我的代码,也许看看我哪里出错了? 谢谢!

1 个答案:

答案 0 :(得分:0)

在“viewDidLoad”方法中,将一行更改为:

self.savedListData = [[NSArray alloc] initWithArray:listData];

除了“listData”需要 某事。 “self.listData”可能是那个,但在我第一次通过时,我看不到填充或设置“self.listData”的位置。

对于属性,您通常使用“self.”来访问(设置和获取)所有属性的所有内容,但init方法除外。

表格没有显示任何内容的原因是因为您的“numberOfRowsInSection”方法可能会返回零。设置一个断点并检查它。

相关问题