我正在努力做到这一点,以便当我在 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) 当表中加载了这些对象时,我如何向目标 View 发送有关发送对象的消息?
提前致谢
最佳答案
它非常简单和直接。只需使用您创建的 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 委托(delegate)函数中,您可以使用以下代码
- (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/