我需要过滤我正在使用的数组,因此它不会在我的leafs
中显示所有结果(只想显示100个UITableView
的前20个)。< /强>
无法弄清楚如何做到这一点。如果您想要发布更多代码,请告诉我们!
(我正在使用RestKit,从API中提取,并且已经使对象映射正常工作)
ViewController.m
@property (strong, nonatomic) NSArray *springs;
@property (strong, nonatomic) NSMutableArray *leafs;
- (void)viewDidLoad
{
[super viewDidLoad];
[[RKObjectManager sharedManager]
loadObjectsAtResourcePath:@"/ss/?apikey=xx"
usingBlock:^(RKObjectLoader *loader) {
loader.onDidLoadObjects = ^(NSArray *objects){
springs = objects;
// @cream-corn this is the for statement you suggested, but I can't finish it
for (Sport *sport in objects){
for (Leaf *leaf in spring.leafs){
if (leaf.abbreviation isEqualToString:@"ap"){
// This is where I can't figure out what to put
[];
}
}
}
[_tableView reloadData];
// @cream-corn this is where I log that I'm getting correct data
for (Spring *sppring in objects){
NSLog(@"%@", spring.name);
for (Leaf *leaf in spring.leafs){
NSLog(@" %@ %@", leaf.name, leaf.abbreviation);
}
}
};
[loader.mappingProvider
setMapping:[Spring mapping]
forKeyPath:@"springs"];
loader.onDidLoadResponse = ^(RKResponse *response){
};
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
Spring *spring = [springs objectAtIndex:section];
return spring.leafs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Spring *spring = [springs objectAtIndex:indexPath.section]; // 13 objects
Leaf *leaf = [spring.leafs objectAtIndex:indexPath.row]; // 30 objects
cell.textLabel.text = leaf.shortName;
return cell;
}
答案 0 :(得分:4)
如果前者是正确的(假设此数组是可变的),您可以执行以下操作:(这是psudeocode的一种形式,此代码不会复制/粘贴)< /强>
for(id obj in [myMutableArray reverseObjectEnumerator]) {
if(obj does not meet condition) {
[myMutableArray remove:obj]
}
}
reverseObjectEnumerator
是这个循环中最重要的部分,没有它;它会引发异常,因为你在枚举时发生变异。
如果后者是正确的,你可以这样做:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection (NSInteger)section
{
Spring *spring = [springs objectAtIndex:section];
return MIN(spring.leafs.count, 20);
}
行return MIN(spring.leafs.count,20);
只返回较小的数字,spring.leafs.count
或20