工具栏和TableView令我头疼

时间:2014-11-28 21:26:44

标签: ios objective-c iphone uitableview

我在实现工具栏(导航栏下方)和桌面视图时遇到了问题。在我的故事板中,我创建了一个普通ViewController,然后将其作为NavigationController的一部分,然后添加了UIToolBarUITableView。完成此操作后,我创建了ViewController个文件。一切正常。但是,当我按下工具栏中的按钮时,它被注册为单击一个单元格,从而导致应用程序崩溃。我的知识还不足以让我能够解决这个问题,这就是我在这里问的原因。

此外,如果您滚动工具栏不会保持在顶部。它随桌子一起滚动。

如何解决这两个问题?

#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

这是故事板截图:http://imgur.com/EOe9iQ3

1 个答案:

答案 0 :(得分:2)

您似乎将UITableViewController作为视图控制器,它始终将UITableView作为其主视图,并且无法更改。

你可以做的就是将新的UIViewController拖到你的故事板上,它将以通常的UIView作为主视图。然后将现有表格和工具栏视图剪切/粘贴为新的UIView子视图。

由于工具栏现在不是您的表格视图的子视图,因此它永远不会随之滚动,也不会在其项目上点击时发生任何崩溃。

最后,将新UIViewController的视图中的任何相关插座/操作连接到新的视图控制器,并且不要忘记将后者设置为表视图的委托/数据源(这是自动完成的) for UITableViewController)