2013-04-11 271 views
2

我的故事板是这样的:UITableView的空白单元格

enter image description here

正如你所看到的,我的MapView低于TableView中,然后当我点击搜索栏上的另一个TableView中会出现。

我的代码:

.H

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 

@interface STLMMeetupLocation : UIViewController <UITextFieldDelegate, UITableViewDataSource, UITableViewDataSource, MKMapViewDelegate, CLLocationManagerDelegate, UISearchBarDelegate, UISearchDisplayDelegate> 
@property (weak, nonatomic) IBOutlet MKMapView *mapView; 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar; 
@end 

.M

#pragma mark - Table View 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return 5; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView == self.tableView) 
{ 
    static NSString *cellIdentifier = @"firstTableViewCell"; 
    UITableViewCell *cell = [tableView 
          dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:cellIdentifier]; 
    } 
    cell.textLabel.text = @"This is a First Table View cell"; 
    return cell; 
} 

else { 
static NSString *cellIdentifier = @"searchBarCell"; 
UITableViewCell *cell = [tableView 
         dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:cellIdentifier]; 
} 
cell.textLabel.text = @"This is a Search Bar cell"; 
    return cell; 
} 

} 

我的模拟器第一屏:

enter image description here

我的模拟器上或者第二屏幕:

enter image description here

为什么我的模拟器首先屏幕的的tableView的cell.textLabel.text空白?它不应该有@“这是第一个表格视图单元格”;我知道我在做一些愚蠢的事情!

+0

你的问题是第一次正确吗? - 当出现上面的屏幕时,会出现空白表格视图,并且在搜索栏上单击后可见。 – 2013-04-11 04:15:53

+0

你什么意思是第一次? – user1107173 2013-04-11 04:16:50

+0

没有。通过点击取消按钮,我可以在第一个屏幕和第二个屏幕之间来回切换,并且第一个屏幕上的TableView保持空白。 – user1107173 2013-04-11 04:18:21

回答

2

我把你的代码,并尝试了。完全为我工作。我认为,你写和UISearchBarsearchDisplayController完美的联系,但是,你缺少实际tableView东西。

我建议你只是有一个快速浏览一下THIS教程。我相信你会很快缺少点。

一切顺利。

+0

谢谢你尝试代码!你是对的。由于AKB8085首先回答,所以我必须给他信用。 – user1107173 2013-04-11 14:05:04

0
-(void)viewWillAppear:(BOOL)animated{ 

    [super viewWillAppear:animated]; 
    [self.tableView reloadData]; 

}