【发布时间】:2012-04-11 10:02:23
【问题描述】:
我使用带有故事板的 Xcode 4.3.2,因为我是新手,所以代码知识不是很好。到目前为止,我已经由主视图控制器创建,它链接到包含数据的表视图。但是我不知道如何将它链接到一个新的视图控制器,即如果我将一个新的视图控制器拖到情节提要页面上,我希望能够选择表格视图中的单元格,然后打开一个包含详细信息的新页面。到目前为止,在教程的帮助下我的代码是:-
Viewcontroller.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITabBarDelegate, UITableViewDataSource, UISearchBarDelegate>
{
IBOutlet UITableView * tableView;
IBOutlet UISearchBar * searchBar;
NSArray * allItems;
NSMutableArray * displayItems;
}
@end
Viewcontroller.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
allItems = [[NSArray alloc] initWithObjects:@"K12", @"K14", @"K16", @"K18", @"K42", @"K43", @"K46", @"K53", @"K60", @"K68", @"K81", @"K83", nil];
displayItems = [[NSMutableArray alloc] initWithArray:allItems];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [displayItems count];
}
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [displayItems objectAtIndex:indexPath.row];
return cell;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([searchText length] == 0) {
[displayItems removeAllObjects];
[displayItems addObjectsFromArray:allItems];
} else {
//here
[displayItems removeAllObjects];
for (NSString * string in allItems) {
NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (r.location != NSNotFound) {
[displayItems addObject:string];
}
}
}
[tableView reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {
[searchBar resignFirstResponder];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
我觉得解决应用程序的其余部分只是停留在这一点上并搜索教程查看了各种代码但由于我的知识非常有限我只需要一个傻瓜指南或者如果有人可以告诉我要添加什么代码更好那会很棒。
谢谢大家,我们将不胜感激。
【问题讨论】:
-
您希望根据所选 textLabel 的内容,还是根据所选行来呈现不同的 viewController?看起来行数会有所不同,它们的内容也会有所不同,基于搜索,这会使事情变得有点复杂。仅供参考,不确定在 Storyboard 中是否可行——您很可能必须使用笔尖。
-
另外,这取决于你是否真的需要为不同的选择使用不同的viewControllers,或者你是否可以使用同一个viewController,显示不同的数据。
-
感谢您的回复。是的,每个选定的行都将包含新视图上的不同数据,因此如果我选择“K12”,那么 K12 的信息将显示其自己的视图/页面。例如,如果它被标记为“福特汽车”,那么当您单击它时,它将显示车辆详细信息。至于笔尖,我不确定如何在 4.3.2 Xcode 中添加笔尖文件。我有点希望将一个新的视图控制器拖到情节提要上,然后以某种方式链接到它,但是如果我必须使用 nib 文件,那么任何帮助将不胜感激。再次感谢您的时间。
-
如果可能的话,我可以根据选择的单元格使用相同的视图控制器来显示不同的数据
标签: xcode4.2