【发布时间】:2014-02-21 01:29:29
【问题描述】:
我需要过滤我正在使用的数组,这样它就不会在我的UITableView 中显示所有结果(只想显示 100 个中的前 20 个 leafs)。
不知道该怎么做。如果您想发布更多代码,请告诉我!
(我正在使用 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;
}
【问题讨论】:
标签: ios uitableview nsarray restkit