1

我想TableView根据PickerView视图中的选择填充 a 的内容。我到目前为止的代码是:

- (IBAction)findOwnerButtonPressed:(id)sender
{
    findShip.hidden = NO;
    shipOwners.hidden = NO;

    boatsTableView.hidden = YES;
    boatOwners = [NSArray arrayWithObjects:@"Owner1",@"Owner2",@"Owner3", nil];
}

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

PickerView 数据源

- (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];
}

PickerView 委托

- (void)pickerView:(UIPickerView *)pickerView 
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component
{
    NSString *boatsFromOwner = [boatOwners objectAtIndex:[shipOwners selectedRowInComponent:0]];
    boatsForOwner = [NSMutableArray arrayWithObjects:nil];
    if ([boatsFromOwner isEqualToString:@"Owner1"]) {
        [self.boatsForOwner addObject:@"Titanic"];
    }
    NSLog(@"%@",boatsFromOwner);
}

TableView 数据源和委托

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [boatsForOwner count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [boatsForOwner objectAtIndex:indexPath.row];
    return cell;
}

这应该是从 a 归还船东公司PickerWheel,并把他们的船给我。此代码在表中不返回任何内容。当PickerWheel我选择它时记录'Owner1'。

我如何将对象添加到数组或如何初始化表或如何将PickerWheel结果与单词进行比较是我的错误吗?

4

2 回答 2

2

做完之后

[self.boatsForOwner addObject:@"Titanic"];

您将需要手动刷新您的 tableview .. 所以在那之后

[_yourTableView reloadData];

我也会像这样初始化你的数组:

boatsForOwner = [NSMutableArray new];
于 2013-01-10T19:05:21.160 回答
0

我认为首先allocinit数组“boatsForOwner”

将您的选择器didSelectRow 方法替换为

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

    if(boatsForOwner) {
        [boatsForOwner release];
        boatsForOwner = nil;
    }

    boatsForOwner = [NSMutableArray alloc] init];

    if ([boatsFromOwner isEqualToString:@"Owner1"]) {
        [self.boatsForOwner addObject:@"Titanic"];
    }

    NSLog(@"%@",boatsFromOwner);

    [tableObject reloadData];

}

release boatsForOwnerdealloc.

于 2013-01-10T19:05:14.373 回答