部分中的行数 [英] number of rows in section

查看:158
本文介绍了部分中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从plist填充的表格视图.在一个plist中,我有一个Categories数组:

I have a table view which populates from a plist. in a plist I have a Categories array:

Catgeories - Array
  item 0 - Maths
  item 1 - English
  ....

在同一个plist中,我有一个Notes数组

in the same plist i have a Notes array

 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  

我所拥有的部分数量...

in number of sections i have...

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
// Return the number of sections.
return [self.categories count];
 }

在numberOfRowsInSection中,在我将类别数组添加到我可以简单使用的plist之前

In numberOfRowsInSection before i added the categories array to the plist i could simply use

  return self.notes.count; 

会在每个类别中产生重复: http://i.stack.imgur.com/9xScH.png

which produces a duplicate in every category: http://i.stack.imgur.com/9xScH.png

但是现在在这种方法中,我应该期望根据该节返回行数,不确定该怎么做

but now in this method i should expect to return the number of rows based on the section, not sure how to do this

推荐答案

您需要执行以下操作:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return self.notes.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.notes[section].count;
}

您将获得基于部分的笔记数组,并返回该数组的计数.

You get array of you notes base on the section and return count of the array.

//EXTENDED 如果要在类别"数组中包含与数据一样多的部分,请使用:

//EXTENDED If you want to have as many section as data in Category array use:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
    {
        return self.catgeories.count;
    }

self.note数组包含带有Category键的字典对象,我相信您希望显示category等于section category的节的所有数据.为此,您必须过滤notes数组,请尝试以下操作(我假设您在Caregory plist中具有不同的对象):

The self.note array contain dictionaries object with Category key and I believe you want to show all of the data for section where category is equal section category. To do that you have to filter your notes array, try this (I assume that you have distinct objects in 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;
}

这篇关于部分中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆