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 に答える