我试图做到这一点,以便当我在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)并且在表中加载了这些对象后,如何向发送该对象的对象的目标视图发送消息?
提前致谢
最佳答案
它非常简单直接。只需使用您创建的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];
}
关于ios - 用NSObject替换NSMutableArray并在UITableView中读取它,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14314919/