ios - UITableView 空白单元格

标签 ios uitableview

我的 Storyboard如下所示:

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 是空白的?它不应该有@“这是第一个 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/

相关文章:

ios - 使用 Storyboard的 UItableView 的空网格

ios - 向以编程方式添加的 subview 添加右对齐约束

objective-c - 当我不知道 UITableView 有多少个部分时,如何重新加载 UITableView 的数据?

ios - 无法使用 json 数据填充 UITableview

ios - 在 iOS 中显示相同文本的多个信息窗口

ios - CVBuffer 和 CVImageBuffer 之间的关系是什么

ios - 如何保存照片和文字

android - 移动应用程序上的 Smooch Rich Messaging

ios - Swift 和核心数据 - 数组和单元格基础知识

ios - UITableViewCell 可重用性问题。修改一个细胞会影响其他细胞