UITableView没有填满数据

时间:2014-11-29 13:07:20

标签: ios objective-c iphone uitableview

我在实施工具栏(导航栏下方)和桌面视图时遇到了问题。在我的故事板中,我创建了一个普通ViewController,然后将其作为NavigationController的一部分,然后添加了UIToolBarUITableView。完成此操作后,我创建了ViewController个文件。但是,tableview没有填满数据。它只是空行...

ViewController.m

#import "TableViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+AFNetworking.h"
#import "Ninja.h"
#import "DetailViewController.h"
#import "AMSlideMenuMainViewController.h"
#import "UIViewController+AMSlideMenu.h"
#import "UIImageView+WebCache.h"
#import "NSString+FontAwesome.h"

@interface TableViewController ()

@property (nonatomic, strong)NSArray *ninjas;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *movies;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *shows;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *profile;

@property (weak, nonatomic) IBOutlet UIToolbar *mainToolBar;

@end

@implementation TableViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setToolbarHidden:YES animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Your watched movies";
    [self addRightMenuButton];

    [self loadNinjas];

    [self.movies setTitleTextAttributes:@{
                                          NSFontAttributeName: [UIFont fontWithName:@"FontAwesome" size:24.0],
                                          NSForegroundColorAttributeName: self.view.tintColor
                                          } forState:UIControlStateNormal];
    [self.movies setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-dot-circle-o"]];

    [self.shows setTitleTextAttributes:@{
                                           NSFontAttributeName: [UIFont fontWithName:@"FontAwesome" size:24.0],
                                           NSForegroundColorAttributeName: self.view.tintColor
                                           } forState:UIControlStateNormal];
    [self.shows setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-pencil-square-o"]];

    [self.profile setTitleTextAttributes:@{
                                          NSFontAttributeName: [UIFont fontWithName:@"FontAwesome" size:24.0],
                                          NSForegroundColorAttributeName: self.view.tintColor
                                          } forState:UIControlStateNormal];
    [self.profile setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-plus"]];

    self.mainToolBar.barTintColor = [UIColor whiteColor];
    self.mainToolBar.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.mainToolBar.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
    self.mainToolBar.layer.shadowRadius = 3.0f;
    self.mainToolBar.layer.shadowOpacity = 1.0f;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.ninjas.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.textLabel.text = [self.ninjas[indexPath.row] name];

     NSString *imageUrl = [NSString stringWithFormat: @"%@", [self.ninjas[indexPath.row] thumbnail]];

    [cell.imageView setImageWithURL:[NSURL URLWithString:imageUrl]
                   placeholderImage:[UIImage imageNamed:@"50-50.jpg"]];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0f;
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    DetailViewController *detailvc = [segue destinationViewController];

    // Pass the selected object to the new view controller.
    NSIndexPath *index = self.tableView.indexPathForSelectedRow;
    Ninja *ninja = self.ninjas[index.row];

    detailvc.ninja = ninja;
}


- (void)loadNinjas {

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/movies"]];


    NSMutableURLRequest *mutableRequest = [request mutableCopy];

    request = [mutableRequest copy];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {


        NSArray *jsonArray = (NSArray *)[responseObject objectForKey:@"data"];

        NSMutableArray *tempNinjas = [[NSMutableArray alloc] init];

        for (NSDictionary *dic in jsonArray) {
            Ninja *ninja = [[Ninja alloc] initWithDictionary:dic];
            [tempNinjas addObject:ninja];
        }


        self.ninjas = [[NSArray alloc] initWithArray:tempNinjas];
        tempNinjas = nil;

        [self.tableView reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Shows"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
        [alertView show];
    }];

    [operation start];
}


@end

ViewController.h

#import <UIKit/UIKit.h>

@interface TableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>             {
    IBOutlet UITableView *tableView;
}

@property (nonatomic, retain) UITableView *tableView;

@end

我不知道出了什么问题,也许你们这样做了?

1 个答案:

答案 0 :(得分:1)

在您的代码中使用此代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
}