2

我正在努力做到这一点,以便当我在 picherWheel 上选择船主时,我会将他们的船放在 tableView 中。目前,我正在通过在选择器上提供带有 if-else 系列的 NSMutableArray 来做到这一点,就像这样

-(void)updatePicker
{
NSString *boatsFromOwner = [boatOwners objectAtIndex:[shipOwners selectedRowInComponent:0]];

boatsForOwner = [[NSMutableArray alloc] init];
if ([boatsFromOwner isEqualToString:@"Owner1"]) {
    [self.boatsForOwner addObject:@"Titanic1"];
    [self.boatsForOwner addObject:@"Titanic2"];
    [self.boatsForOwner addObject:@"Titanic3"];
    [self.boatsForOwner addObject:@"Titanic4"];
    [self.boatsForOwner addObject:@"Titanic5"];
    [self.boatsForOwner addObject:@"Titanic6"];
    [self.boatsForOwner addObject:@"Titanic7"];
    }
if ([boatsFromOwner isEqualToString:@"Owner2"]) {
    [self.boatsForOwner addObject:@"Titanic1"];
    [self.boatsForOwner addObject:@"Titanic2"];
    [self.boatsForOwner addObject:@"Titanic3"];


    }

然后将其读取到这样的 tableView 中:

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

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

SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];   


cell.nameLabel.text = [boatsForOwner objectAtIndex:indexPath.row];
cell.moreInfoLabel.text = [boatsForOwner objectAtIndex:indexPath.row];

return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [self.boatsTableView cellForRowAtIndexPath:indexPath];

[self performSegueWithIdentifier:@"boatInfoSegue" sender:cell];


[tableView deselectRowAtIndexPath:indexPath animated:YES];

现在,这工作正常。但我认为使用 NSObjects 存储有关船只的信息会很聪明,因为我将在多个地方需要它。所以我做了一些船对象,像这样:

cargoShips* Titanic = [[cargoShips alloc]init];
Titanic.name = @"Titanic1";
Titanic.size = @"1000";
Titanic.owner = @"Owner1";
cargoShips* Titanic2 = [[cargoShips alloc]init];
Titanic2.name = @"Titanic2";
Titanic2.size = @"2000";
Titanic2.owner = @"Owner2";

这可以正确记录并且效果很好。这是第一个问题:

1)如何将pickerWheel上的对象链接到* .owner,所以当我在轮子上选择“Owner1”时,我会得到所有船的标签*.owner = @"Owner1"

2)如何使用这些船的 *.name 填充 UITableView?

3)当表中加载了这些对象时,我怎样才能向目标视图发送关于发送什么对象的消息呢?

提前致谢

4

1 回答 1

1

它非常简单直接..只需使用您创建的 CargoShips 类的对象..然后这样做..

-(void)updatePicker
{
NSString *boatsFromOwner = [boatOwners objectAtIndex:[shipOwners selectedRowInComponent:0]];

boatsForOwner = [[NSMutableArray alloc] init];
if ([boatsFromOwner isEqualToString:@"Owner1"]) {
  cargoShips* Titanic = [[cargoShips alloc]init];
  Titanic.name = @"Titanic1";
  Titanic.size = @"1000";
  Titanic.owner = @"Owner1";

  [self.boatsForOwner addObject:Titanic];

  Titanic = [[cargoShips alloc]init];
  Titanic.name = @"Titanic2";
  Titanic.size = @"2000";
  Titanic.owner = @"Owner2";

  [self.boatsForOwner addObject:Titanic];

  Titanic = [[cargoShips alloc]init];
  Titanic.name = @"Titanic3";
  Titanic.size = @"3000";
  Titanic.owner = @"Owner3";

  [self.boatsForOwner addObject:Titanic];

}else if ([boatsFromOwner isEqualToString:@"Owner2"]) {
  cargoShips* Titanic = [[cargoShips alloc]init];
  Titanic.name = @"Titanic1";
  Titanic.size = @"1000";
  Titanic.owner = @"Owner1";

  [self.boatsForOwner addObject:Titanic];

  Titanic = [[cargoShips alloc]init];
  Titanic.name = @"Titanic2";
  Titanic.size = @"2000";
  Titanic.owner = @"Owner2";

  [self.boatsForOwner addObject:Titanic];

}

}

在 TableView 委托函数中,您可以使用以下代码

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

SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];   

cargoShips* Titanic=[boatsForOwner objectAtIndex:indexPath.row];//You can get the object reference like this 
cell.nameLabel.text = Titanic.name;
cell.moreInfoLabel.text = Titanic.owner;

return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{

cargoShips* Titanic=[boatsForOwner objectAtIndex:indexPath.row]; //Use the Titanic object for further processing..
UITableViewCell *cell = [self.boatsTableView cellForRowAtIndexPath:indexPath];

[self performSegueWithIdentifier:@"boatInfoSegue" sender:cell];


[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
于 2013-01-14T09:30:22.440 回答