部分中的行数
我有一个从plist填充的表视图。 在plist中我有一个Categories数组:
Catgeories - Array item 0 - Maths item 1 - English ....
在同一个plist我有一个Notes数组
Notes - Array item 0 - Dictionary Title - My maths note 1 Text - This is algebra Category - Maths item 1 - Dictionary Title - My English Text - This is algebra Category - English
我有几个部分…
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return [self.categories count]; }
在numberOfRowsInSection之前,我将类别数组添加到plist我可以简单地使用
return self.notes.count;
它会在每个类别中生成一个副本: http : //i.stack.imgur.com/9xScH.png
但现在在这个方法中,我应该期望返回基于该部分的行数,不知道如何做到这一点
你需要做这样的事情:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; { return self.notes.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.notes[section].count; }
根据该部分获取笔记数组并返回该数组的计数。
// EXTENDED如果你想在Category数组中使用与数据一样多的部分,使用:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; { return self.catgeories.count; }
self.note数组包含带有Category键的字典对象,我相信您希望显示类别与节类别相同的节的所有数据。 要做到这一点,你必须过滤你的笔记数组,试试这个(我假设你在Caregory plist中有不同的对象):
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Get category NSString *cat = self.catgeories[section]; //Filter notes array NSPredicate *pred = [NSPredicate predicateWithFormat:@"Category =[cd] %@", cat]; NSArray * catArray = [self.notes filteredArrayUsingPredicate:pred]; return catArray.count; }
我已经编辑了答案。 如果你想访问大量的数据,我build议你将数据存储在核心数据而不是plist,为了获得每个部分的行数,你可以使用这个
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // check if the count of the array is greater than number of sections if (self.notes.count > section) return [[[self.notes objectAtIndex:section]allObjects]count]; }
希望能帮助到你 :)
如果您想在每个部分返回不同数量的行,您可以尝试这样:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section { int rows=0; if(section==0) { rows=1; } if(section==1) { rows=2; } rerurn rows; }
- 通过Amazon SNS和Unity的iOS APNS – 无法创build开发iOS证书
- Xamarin Ios – 仅在一侧创build圆形button
- 在Xcode 8中隐式解包可选分配
- iOS 8.1企业分布
- 更改所选UITableViewCell的边框颜色
- 如何在iOS SceneKit中将.dae文件加载到SCNNode中?
- 如何在不使用AudioSessionSetProperty的情况下路由到kAudioSessionProperty_OverrideCategoryEnableBluetoothInput
- `master` repo需要CocoaPods 0.18.1 –
- ios – 失败的应用程序存储ipv6但无法重新生成