[英]Filter Array results before showing in Table View
I need to filter the array I'm using so it doesn't show all of the results (want to only show first 20 of 100 leafs
) in my UITableView
. 我需要过滤我正在使用的数组,以便它不会在
UITableView
显示所有结果(只希望显示100个leafs
前20个)。
Can't figure out how to do it. 无法弄清楚该怎么做。 Let me know if you would like more code posted!
让我知道您是否想发布更多代码!
(I'm using RestKit, pulling from an API, and already have the Object Mapping working fine) (我使用的是RestKit,是从API中提取的,并且已经可以正常使用对象映射了)
ViewController.m 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;
}
okay, so. 可以,然后呢。 do you want to filter the array specifically?
您是否要专门过滤数组? ie: remove objects that meet a certain condition?
即:删除满足特定条件的对象? OR just display the first 20 in the table view?
还是仅在表格视图中显示前20个?
If the former is correct (assuming that this array is mutable) you can do something like this: (this is a form of psudeocode this code won't copy/paste) 如果前者是正确的(假设此数组是可变的) ,则可以执行以下操作:( 这是一种伪代码,此代码不会复制/粘贴)
for(id obj in [myMutableArray reverseObjectEnumerator]) {
if(obj does not meet condition) {
[myMutableArray remove:obj]
}
}
reverseObjectEnumerator
is the most important piece of this loop, without it; reverseObjectEnumerator
没有它, reverseObjectEnumerator
是该循环中最重要的部分。 it will throw an exception because you are mutating whilst enumerating. 它将引发异常,因为您在枚举时正在变异。
If the latter is correct you can do this: 如果后者是正确的,您可以这样做:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection (NSInteger)section
{
Spring *spring = [springs objectAtIndex:section];
return MIN(spring.leafs.count, 20);
}
the line return MIN(spring.leafs.count,20);
该行
return MIN(spring.leafs.count,20);
just returns the smaller number, either spring.leafs.count
or 20
只会返回较小的数字,即
spring.leafs.count
或20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.