user1107173
user1107173

Reputation: 10744

UITableView Blank Cells

My Storyboard looks like this:

enter image description here

As you can see, I have a TableView below the MapView and then another TableView will appear when I click on the search bar.

My Code:

.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;
}

}

My Simulator First Screen:

enter image description here

My Simulator Second Screen:

enter image description here

Why is my simulators First screen's tableView's cell.textLabel.text Blank? Shouldn't it have @"This is the First Table View cell"; I know I'm doing something stupid!

Upvotes: 2

Views: 3191

Answers (3)

Sovannarith
Sovannarith

Reputation: 616

-(void)viewWillAppear:(BOOL)animated{

   [super viewWillAppear:animated];
   [self.tableView reloadData];

}

Upvotes: 0

viral
viral

Reputation: 4208

I took your code and tried it. Works perfectly for me. I think you have written and connected perfectly for UISearchBar and searchDisplayController. But, You are missing something with the actual tableView.

I suggest you to just have a quick look at THIS tutorial. I am sure you will get the missing point quickly.

All the best.

Upvotes: 2

AKB
AKB

Reputation: 122

The only possible reason in your case is that you had not connected your datasource and delegate properly. If you've connected already, but still getting problems, then just disconnect all the connections and connect them again. Here's a sample image....

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

And also, you had used UITableViewDataSource twice.... :(

Upvotes: 3

Related Questions