我需要过滤我正在使用的数组,这样它就不会在我的 UITableView 中显示所有结果(只想显示 100 个
.leafs
中的前 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)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
Spring *spring = [springs objectAtIndex:section];
return spring.leafs.count;
}
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;
}
好的,所以。你想专门过滤数组吗?即:删除满足特定条件的对象? 或者只在表格 View 中显示前 20 个?
如果前者是正确的(假设这个数组是可变的),你可以这样做:(这是一种伪代码,此代码不会复制/粘贴)
for(id obj in [myMutableArray reverseObjectEnumerator]) {
if(obj does not meet condition) {
[myMutableArray removebj]
}
}
reverseObjectEnumerator
是这个循环中最重要的部分,没有它;它会抛出异常,因为您在枚举时发生了变异。
如果后者是正确的,你可以这样做:
- (NSInteger)tableViewUITableView *)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
关于ios - 在表格 View 中显示之前过滤阵列结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21923401/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |