【发布时间】:2013-04-11 04:05:51
【问题描述】:
我的故事板如下所示:
如您所见,我在 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;
}
}
我的模拟器第一个屏幕:
我的模拟器第二屏:
为什么我的模拟器First screen的tableView的cell.textLabel.text是空白的?它不应该有@“这是第一个表视图单元格”;我知道我在做一些愚蠢的事情!
【问题讨论】:
-
你的问题是第一次吧? - 当上面的屏幕出现时,它会出现空白表格视图,点击搜索栏单元格后可见。
-
第一次是什么意思?
-
不。我可以通过点击取消按钮在第一个屏幕和第二个屏幕之间来回切换,第一个屏幕上的 TableView 保持空白。
-
可能问题出在“if (tableView == self.tableView)”这一行,所以只需尝试将 [self.tableView reloadData] 放在 viewWillAppear 上并检查
-
@user1107173 你在你的.storyboard 中连接了tableview 的数据源和委托吗?或者,您需要在 viewDidLoad 中写入 [self.tableView setDataSource:self]; [self.tableView setDelegate:self];
标签: ios uitableview