我的 Storyboard如下所示:
如您所见,我在 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;
}
}
我的模拟器第一屏幕:
我的模拟器第二个屏幕:
为什么我的模拟器第一个屏幕的 tableView 的 cell.textLabel.text 是空白的?它不应该有@“这是第一个 TableView 单元格”吗?我知道我在做一些愚蠢的事情!
最佳答案
您的情况唯一可能的原因是您没有正确连接数据源和委托(delegate)。如果您已经连接,但仍然遇到问题,则只需断开所有连接并重新连接即可。这是一个示例图像......
https://www.dropbox.com/s/y51astwgjveeixm/Screen%20Shot%202013-04-11%20at%2010.41.02%20AM.png
而且,您已经使用了 UITableViewDataSource 两次......:(
关于ios - UITableView 空白单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15940589/