简体   繁体   中英

UITableView cellForRowAtIndexPath is not called

It's table view using UISearchBar and UITableView . numberOfRowsInSection called. but cellForRowAtIndexPath is not being called. I don't understand why cellForRowAtIndexPath is not called.

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

You have to also implement

-tableView:numberOfRowsInSection:

So UITableView can determine how often it has to call -tableView:cellForRowAtIndexPath: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM