gpt4 book ai didi

ios - 工具栏和 TableView 让我头疼

转载 作者:行者123 更新时间:2023-11-28 21:54:08 24 4
gpt4 key购买 nike

我在实现工具栏(在我的导航栏下方)和表格 View 时遇到了问题。在我的 Storyboard中,我创建了一个普通的 ViewController,然后将其作为 NavigationController 的一部分,然后添加了一个 UIToolBar 和一个 UITableView。完成后,我创建了 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

这是 Storyboard截图:http://imgur.com/EOe9iQ3

最佳答案

看起来你有 UITableViewController 作为你的 View Controller ,它总是有 UITableView 作为它的主视图并且不能改变。

你可以做的只是将新的 UIViewController 拖到你的 Storyboard上,它会以通常的 UIView 作为它的主视图。然后将现有的表格和工具栏 View 剪切/粘贴为新的 UIView subview 。

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

最后,将新 UIViewController 的 View 中的任何相关导出/操作连接到新 View Controller ,并且不要忘记将后者设置为 TableView 的委托(delegate)/数据源(这是为 UITableViewController 自动完成的) )

关于ios - 工具栏和 TableView 让我头疼,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com