【问题标题】:UITableView Blank CellsUITableView 空白单元格
【发布时间】: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


【解决方案1】:

在您的情况下,唯一可能的原因是您没有正确连接数据源和委托。如果您已经连接,但仍然遇到问题,则只需断开所有连接并重新连接。这是一个示例图像....

https://www.dropbox.com/s/y51astwgjveeixm/Screen%20Shot%202013-04-11%20at%2010.41.02%20AM.png

而且,你已经使用了 UITableViewDataSource 两次.... :(

【讨论】:

    【解决方案2】:

    我拿了你的代码并试了一下。非常适合我。我认为你已经为UISearchBarsearchDisplayController 编写和连接了完美的东西。但是,实际的tableView 缺少一些东西。

    我建议您快速浏览一下THIS 教程。我相信你会很快找到遗漏的地方。

    一切顺利。

    【讨论】:

    • 感谢您试用代码!你说的对。既然AKB8085先回答了,那我就给他点赞吧。
    【解决方案3】:
    -(void)viewWillAppear:(BOOL)animated{
    
       [super viewWillAppear:animated];
       [self.tableView reloadData];
    
    }
    

    【讨论】: