這是使用UISearchBar
和UITableView
的表格視圖。調用 numberOfRowsInSection
。但沒有調用cellForRowAtIndexPath
。 我不明白爲什麼cellForRowAtIndexPath
沒有被調用。不調用UITableView cellForRowAtIndexPath
RegistMovieViewController.h
#import <UIKit/UIKit.h>
@interface RegistMovieViewController : UIViewController <UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
{
UITableView *searchResultView;
NSMutableArray *searchResult;
}
@end
RegistMovieViewController.m
//
// RegistMovieViewController.m
//
#import "RegistMovieViewController.h"
#import <TBXML.h>
#import <TBXML+HTTP.h>
#import <TBXML+NSDictionary.h>
@interface RegistMovieViewController()
@end
@implementation RegistMovieViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButtonItem;
UIBarButtonItem *saveButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)];
self.navigationItem.rightBarButtonItem = saveButtonItem;
CGFloat topOffset = 0;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
[self.view setTintColor:[UIColor blueColor]];
topOffset = [[UIApplication sharedApplication] statusBarFrame].size.height + self.navigationController.navigationBar.frame.size.height;
#endif
searchResult = [[NSMutableArray alloc] init];
// searchBar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, topOffset, self.view.frame.size.width, 0)];
[searchBar setDelegate:self];
[searchBar sizeToFit];
[self.view addSubview:searchBar];
// tableView
CGFloat y = searchBar.frame.origin.y + searchBar.frame.size.height;
searchResultView = [[UITableView alloc] initWithFrame:CGRectMake(0, y, self.view.frame.size.width, self.view.frame.size.height - y) style:UITableViewStylePlain];
[searchResultView setDataSource:self];
[searchResultView setDelegate:self];
[self.view addSubview:searchResultView];
[searchBar becomeFirstResponder];
}
- (void)save {
}
- (void)cancel {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UISearchBar Delegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSString *urlString = [NSString stringWithFormat:@"http://openapi.naver.com/search?key=65d378a85138040e682d458320ef35d9&target=movie&query=%@", [searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"searchBarSearchButtonClicked : %@", urlString);
TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) {
TBXMLElement *rootElement = tbxmlDocument.rootXMLElement;
TBXMLElement *errorElement = [TBXML childElementNamed:@"error_code" parentElement:rootElement];
if (errorElement != nil) {
TBXMLElement *errorMsgElement = [TBXML childElementNamed:@"message" parentElement:rootElement];
NSLog(@"Error[%@]:%@", [TBXML textForElement:errorElement], [TBXML textForElement:errorMsgElement]);
} else {
TBXMLElement *channelElement = [TBXML childElementNamed:@"channel" parentElement:rootElement];
TBXMLElement *totalElement = [TBXML childElementNamed:@"total" parentElement:channelElement];
NSString *totalCount = [TBXML textForElement:totalElement];
if ([totalCount integerValue] == 0) {
NSLog(@"No Data!");
} else {
TBXMLElement *itemElement = [TBXML childElementNamed:@"item" parentElement:channelElement];
[searchResult removeAllObjects];
do {
//TBXMLElement *titleElement = [TBXML childElementNamed:@"title" parentElement:itemElement];
//NSLog(@"title:%@", [TBXML textForElement:titleElement]);
NSDictionary *itemDic = [TBXML dictionaryWithXMLNode:itemElement];
[searchResult addObject:itemDic];
} while ((itemElement = [TBXML nextSiblingNamed:@"item" searchFromElement:itemElement]) != nil);
NSLog(@"Reload!");
[searchResultView reloadData];
}
}
};
TBXMLFailureBlock failureBlock = ^(TBXML *tbxmlDocument, NSError *error) {
NSLog(@"Error! %@ %@", [error localizedDescription], [error userInfo]);
};
[TBXML tbxmlWithURL:url success:successBlock failure:failureBlock];
}
#pragma mark - UITableView Datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@":: cellForRowAtIndexPath");
static NSString *CellIdentifier = @"MovieCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"TEST";
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@":: numberOfRowsInSection : %d", [searchResult count]);
return [searchResult count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@":: didSelectRowAtIndexPath");
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
@end
檢查您的numberOfRowsInSection是否返回適當數量的計數?如果這是非零和積極的只有它會調用cellforrow – 2014-09-04 09:09:42