number of rows in section

隐身守侯 提交于 2020-01-17 07:10:23

问题


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
  ....

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];
 }

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

  return self.notes.count; 

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


回答1:


You need to do something like that:

- (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 If you want to have as many section as data in Category array use:

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

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;
}



回答2:


I have edited the answer. If you wanna access large amount of data, i suggest that you store your data in core data instead of plist, For getting number of rows for each section you can use this

- (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];
}

Hope it helps :)




回答3:


You can try like this if you want to return different number of rows in each section:-

 - (NSInteger)tableView:(UITableView 
   *)tableView numberOfRowsInSection:
   (NSInteger)section
 { 
 int rows=0;
  if(section==0)
   {
    rows=1;
    }

   if(section==1)
  {
   rows=2;
  }
rerurn rows;
  }


来源:https://stackoverflow.com/questions/20454366/number-of-rows-in-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!