工具栏和TableView让我头疼

用户名

我在实现工具栏(在导航栏下方)和表格视图时遇到麻烦。在我的情节提要中,我创建了一个法线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

麦克斯·谢泰

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

您可以做的就是将新的UIViewController拖到情节提要中,并将其与通常的UIView一起作为其主视图。然后将现有的表和工具栏视图剪切/粘贴为新的UIView子视图。

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

最后,将新UIViewController的视图中的所有相关出口/动作连接到新视图控制器,并且不要忘记将后者设置为表视图的委托/数据源(UITableViewController自动完成)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

带有导航栏和工具栏的布局TableView

来自分类Dev

工具栏和NavigationDrawer

来自分类Dev

TableView中的工具栏(快速)

来自分类Dev

ActionBar与工具栏或ActionBar和工具栏

来自分类Dev

如何使我的导航栏和底部的工具栏成为单一颜色?

来自分类Dev

工具栏的高度和阴影错误

来自分类Dev

Android NavigationDrawer和工具栏

来自分类Dev

折叠工具栏和DrawerLayout

来自分类Dev

工具栏和标签之间的线

来自分类Dev

Android工具栏和SlidingTabLayout大小

来自分类Dev

折叠工具栏和DrawerLayout

来自分类Dev

未找到 RecyclerView 和工具栏

来自分类Dev

覆盖在工具栏和晶圆厂之上

来自分类Dev

MS Access 2013,我的工具栏和vba编辑器在哪里?

来自分类Dev

如何让我的搜索栏覆盖工具栏中的标题?

来自分类Dev

setSupportActionBar(工具栏工具栏)在我的材料项目中不起作用

来自分类Dev

工具栏和操作栏未显示在活动中

来自分类Dev

设置工具栏颜色和导航栏后退按钮

来自分类Dev

Android隐藏操作栏和工具栏

来自分类Dev

自动设置标签栏按钮和工具栏标题?

来自分类Dev

如何使qterminal的工具栏和标签栏变暗?

来自分类Dev

在哪里添加工具栏和导航栏

来自分类Dev

如何使用工具栏更改工具栏名称和放置图标

来自分类Dev

如何在工具栏的中间和顶部放置工具栏(vue,vuetify)

来自分类Dev

使用MVVM动态创建工具栏和工具栏项

来自分类Dev

折叠工具栏和ImageView-工具栏未显示

来自分类Dev

工具栏盲区

来自分类Dev

Javascript工具栏

来自分类Dev

Android工具栏没有做我想要的