2013-04-11 266 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]; 

}