2013-01-09 35 views
1

这是一个非常宽泛的问题,而不是关于单个问题的具体问题。如果你不想帮助其中的一个,这里是你的警告!选择UITableView里面的对象由UIPickerView选择

我有一个查看pickerWheel。在这个选择器上,我想要一个货船船东名单。当我按下OK按钮时,我想要一个UITableView由该主人拥有的船只填充。不多,大约有5艘。我希望tableView与拾取器处于相同的视图。

当我按下一艘船在列表中,我要被引导到另一种观点认为,我可以从其他视图中添加变量数,像马车负荷等

我知道如何制造和填充和提取数据从选择器,我不知道如何使用这些数据以某种方式填充表,我不知道如何使用表上的对象将我链接到一个视图,我可以根据发送它的人来设置不同的值。

我不擅长编程,用代码,使fullout的解决方案将是巨大的:)

编辑:代码,我至今

.h file 
#import <UIKit/UIKit.h> 
#import "StartupViewController.h" 

@interface CargoViewController : UIViewController 

@property(strong,nonatomic) IBOutlet UILabel *testLabel; //label I use to test stuff instead of LOG 
@property(strong,nonatomic) IBOutlet UIButton *findOwner; //button to summon pickerWheel 
@property(strong,nonatomic) IBOutlet UIButton *findShip; //Button to confirm selection in picker and unhide tableView 
@property(strong,nonatomic) NSArray *boatOwners; //Array for boat owners. 
@property(strong,nonatomic) NSMutableArray *boatsForOwner; //My idea was that when you select a owner, i have a if-else series, so say i select "Bob", I code to add his boats to the array. Does this work? IDK 
@property(strong,nonatomic) IBOutlet UITableView *boatsTableView; //Table view 
-(IBAction)findOwnerButtonPressed:(id)sender; 
-(IBAction)findShipButtonPressed:(id)sender; 
@property(strong, nonatomic) IBOutlet UIPickerView *shipOwners; //ship owner picker wheel 

@end 

.m文件:

#import "CargoViewController.h" 

@interface CargoViewController() 


@end 

@implementation CargoViewController 
@synthesize testLabel, findOwner, findShip,shipOwners, boatsTableView; 
@synthesize boatsForOwner, boatOwners; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
[self getDataFromDensity ]; 

boatOwners = @[@"owner1", @"owner2", @"owner3"]; 

} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 
-(void) getDataFromDensity 
{ 
NSString *getData = [[NSUserDefaults standardUserDefaults] 
        objectForKey:@"globalMathString"]; //Gets a number from another view, wanted to use in the information about boat view later on. 
testLabel.text = getData; 
[boatsForOwner setValue:@"5" forKey:getData]; 
} 
-(IBAction)findOwnerButtonPressed:(id)sender 
{ 
findShip.hidden = NO; 
shipOwners.hidden = NO; 

boatsTableView.hidden = YES; 
} 
-(IBAction)findShipButtonPressed:(id)sender 
{ 
shipOwners.hidden = YES; 
findShip.hidden = YES; 
boatsTableView.hidden = NO; 

} 
#pragma mark - 
#pragma mark PickerView DataSource 

- (NSInteger)numberOfComponentsInPickerView: 
(UIPickerView *)pickerView 
{ 
return 1; 
} 
- (NSInteger)pickerView:(UIPickerView *)pickerView 
numberOfRowsInComponent:(NSInteger)component 
{ 
return [boatOwners count]; 
} 
- (NSString *)pickerView:(UIPickerView *)pickerView 
     titleForRow:(NSInteger)row 
     forComponent:(NSInteger)component 
{ 
return [boatOwners objectAtIndex:row]; 
} 


#pragma mark - 
#pragma mark PickerView Delegate 
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row 
    inComponent:(NSInteger)component 
{ 
NSString *boatsFromOwner = [boatOwners objectAtIndex:[shipOwners selectedRowInComponent:0]]; 



} 
+0

你正在读书吗? –

+0

我不学习。我知道什么是随机tuts我在谷歌上找到,ray wenderlich的“Cocos2d for beginners”或什么和从techotopia的Obj-C基础知识 –

+0

所以我没有在这个工作,所以这就是为什么即时通讯如此糟糕 –

回答

1

在假冒我认为你会需要这样的东西。

cargoshipOwner.h // Class that holds all details for a cargoship owner. 

类可能有一个属性

NSMutableArray *listOfCurrentlyOwnedShips; 

其中填充:

cargoShip.h // CargoShip class. 

NSInteger carriageLoad; 
UIColor *shipColor; 
NSString *model; 
NSString *name; 

这听起来像你想与充满cargoshipOwners当一个NSMutableArray来填充pickerview被选中要通过 对象的属性listOfCurrentlyOwnedShips并填充UITableView

运行完毕后,可以使用cargoship对象轻松推送到detailView并对其进行处理。

一旦你建立了你的UITableViewDelegate,它的功能正常,你应该在你的.m文件中具有以下功能。

你需要使用的功能cellForRowAtIndexPath:画出你的细胞,一个例子可以是这样的:

-(UItableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath 
{ 

    // Create a cell with 300 width and 80 height. 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectmake(0,0,300,80)]; 

    // Create a label. 
    UILabel *lblCargoShipOwner = [[UILabel alloc] initWithFrame:CGRectMake(10,12,200,20)]; 
    lblCargoShipOwner.tag = 1; 
    lblCargoShipOwner.textColor = [UIColor blackColor]; 
    lblCargoShipOwner.backGroundColor = [UIColor clearColor];  


    // Add it to your cell. 
    [cell.contentView addSubView:lblCargoShipOwner]; 


    // Get the label. 
    UILabel *textLabel = (UILabel *)[cell viewWithtag:1]; 

    // Add the owner's name to the label. 
    textLabel.text = [[listOfCargoShipOwners objectAtIndex:indexPath.row]sName]; 

    // important to note is that listOfCargoShipOwners is your datasource. 
} 

当你填写好UITableView您可以点击一个单元格,并指定中tableView:didSelectRowAtIndexPath:发生了什么举个例子:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexpath *)indexPath 
{ 

    // Push the owner's list of ships to a UITableViewController 

    DetailViewController *dvc = [[DetailViewController alloc] initWithStyle:UITableViewStylePlain andCargoShipList: [[listOfCargoShipOwners objectAtindex:indexPath.row]listOfCurrentlyOwnedShips]]; 



} 

免责声明:这写没有任何形式的IDE,因此它可能会或可能不会进行编译的。这也只是一个例子,代码本身可能会或可能不会针对您的特定问题进行优化。

+0

这似乎与我想象的相同。我最大的问题仍然是如何在视图中填充列表,而不是使用不同的内容填充全屏列表。几乎所有我觉得这个话题都离我所需要的很远的地方。加上我缺乏关于如何使用表的知识,只是一般obj-c –

+0

我将在稍后扩展我的帖子,更详细地介绍如何填充和推送对象。 – doge

+0

很酷,谢谢! –

1

两个提示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 

所以你可以改变:

如果要实现UITableViewDelegate protocol,那么你将被当用户在表视图中选择行通知相应的视图内容,然后在您的UINavigationController中推送该视图。

2.检查UITableViewDataSource protocol知道如何将内容放在表格视图中。

+0

谢谢,多数民众赞成有用的教育我在这个东西 –