- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int count = [entries count];
if (count == 0) {
return kCustomRowCount;
}
return count;
int rowCount;
if (self.isFiltered) {
rowCount = filteredTableData.count;
}
else {
rowCount = allTableData.count;
}
return rowCount;
}
My problem: The first function return count; is needed to fill the parsed data into the tableView. The second one return rowCount; is needed to count the filtered entries for the search. But when I use both, my App dies. When I delete the first part, the search seems to work incorrectly..
Sascha
It sounds like you need to be making use of the UISearchDisplayController. This controller essentially supports an unfiltered and a filtered (searched) list.
You can then use something like the following in your numberOfRowsInSection:
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(tableView == self.searchDisplayController.searchResultsTableView){
// search view population
return [self.filteredList count];
} else {
return [[self.sectionedList objectAtIndex:section] count];
}
}
Related
I have a tableView with static cells. If one of the cells are out of the view, the cells disappear and everything overlaps. Here's a screenshot:
This is the relevant table view code:
- (BOOL) tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 1;
} else if (section == 1) {
return 4;
} else if (section == 2) {
return 3;
}
return 0;
}
Thanks
I'm creating a recipes app and have run into this semantic issue in the number of rows in section method of my table view. This is the first time I've really worked with table Views and I'm wondering if someone could perhaps see what I've done wrong and point me in the right direction. Thanks!
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (menuInt == 0)
return [soupsArray count];
if (menuInt == 1)
return [saladsArray count];
if (menuInt == 2)
return [appetizersArray count];
if (menuInt == 3)
return [entreeArray count];
if (menuInt == 4)
return [dissertsArray count];
[self.tableView reloadData];
}
What happens if all the if conditions fail? You need to make sure that you at least return an NSInteger even though you are sure one of the if conditions will succeed for sure. That's just how it is.
Also, as Martin R pointed out, you should not have reloadData in the function.
try one of these 2 solutions (the reload data must occur before the return statement also)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int counter = 0;
// Return the number of rows in the section.
if (menuInt == 0)
counter = [soupsArray count];
if (menuInt == 1)
counter = [saladsArray count];
if (menuInt == 2)
counter = [appetizersArray count];
if (menuInt == 3)
counter = [entreeArray count];
if (menuInt == 4)
counter = [dissertsArray count];
[self.tableView reloadData];
return counter;
}
or
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (menuInt == 0) {
[self.tableView reloadData];
return [soupsArray count];
}
else if (menuInt == 1) {
[self.tableView reloadData];
return [saladsArray count];
}
else if (menuInt == 2) {
[self.tableView reloadData];
return [appetizersArray count];
}
else if (menuInt == 3) {
[self.tableView reloadData];
return [entreeArray count];
}
else if (menuInt == 4) {
[self.tableView reloadData];
return [dissertsArray count];
else {
[self.tableview reloadData];
return 0;
}
}
I have a problem which seems to be very simple. I have a UITableView in my APP. In numberOfSectionsInTableView: I set the count of sections to 2. However the tableView shows only the first section. I tried to make the two sections show same contents by writing same codes in both sections, but useless. Then I found a similar question here. But its correct answer is about frame and my layout is a very simple UITableViewController. At last I tried to add NSLog to the three methods. The result is: For case section 1, numberOfSectionsInTableView: and numberOfRowsInSection: are called but cellForRowAtIndex isn't called. I wonder how to explain this.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
int count;
switch (section) {
case 0:
count = [_objectArray count];
break;
case 1:
NSLog(#"section called"); // This line is logged
count = 1;
default:
count = 0;
break;
}
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myCellIdentifier";
static NSString *basicCellIdentifier = #"myBasicCellIdentifier";
switch ([indexPath section]) {
case 0: {
TableViewStoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
// Configure the cell...
int theIndex = [indexPath row];
MyObject *theObj = [_objectArray objectAtIndex:theIndex];
cell.object = theObj;
return cell;
break;
}
case 1: {
NSLog(#"cell called"); //this line isn't logged
for (int i = 0; i < 50; i++) {
NSLog(#"hahaha");
}
MyBasicViewStoryCell *cell = [tableView dequeueReusableCellWithIdentifier:basicCellIdentifier forIndexPath:indexPath];
return cell;
break;
}
default: {
for (int i = 0; i < 50; i++) {
NSLog(#"hahaha1");
}
return nil;
break;
}
}
}
Put break in switch case. You missed break in case 1: so the default will also execute. Thus the count become zero count = 0. Since numberOfRowsInSection: returns zero, cellForRowAtIndexPath: will not call. So put break in case 1: of numberOfRowsInSection:
switch (section) {
case 0:
count = [_objectArray count];
break;
case 1:
NSLog(#"section called"); // This line is logged
count = 1;
break; // Here
default:
count = 0;
break;
}
You don't have a break after you set the count for section 1 and it falls through and is set to zero.
case 1:
NSLog(#"section called"); // This line is logged
count = 1;
break; // always put break!
cellForRowAtIndexPath won't be called if numberOfRowsInSection is not returning a value that is >=1.
I am trying to implement expandable and collapsable sections in my tableView. I am following a tutorial to do it, but I would need your help.
expandedSections is a NSMutableIndexSet and canCollapseSection is a BOOL method declared as follows:
-(BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section {
if (section >0) return YES;
return NO;
}
This is the numberOfRowsInSection original method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self tableView:tableView canCollapseSection:section])
{
if ([expandedSections containsIndex:section])
{
return 5; // return rows when expanded
}
return 1; // only top row showing
}
// Return the number of rows in the section.
return 1;
}
And this is my updated code for the same method. I have also implemented a search bar and NSFetchedResultsController to populate the section headers and rows.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.searchResults count];
}
else {
if ([self tableView:tableView canCollapseSection:section])
{
if ([expandedSections containsIndex:section])
{
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
return 1;}
}
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
The app is not crashing, and the sections are shown, but it is not working as expected. May be you could check my code and tell me what is wrong there.
This is a screenshot from the tableView:
(From my comments:)
The reason that the first section appears to different from the other sections is
that your canCollapseSection method explicitly returns NO for section #0.
There could be another problem in combination with a fetched results controller:
If numberOfRowsInSection returns 1 for a collapsed section (instead of the actual number of rows in that section) then you would also have to modify the FRC delegate methods (so that they do not call insertRows... or deleteRows... for currently "invisible" rows).
Why is my app not showing the expected number of rows? I am testing my tableView and it is showing perfectly the section titles and the expected rows in the sections, but only if populated from text included in a switch loop.
In this case, I am using fixed sections and section titles:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 6;
}
+ (NSString*) titleForHeaderForSection:(int) section
{
switch (section)
{
case 0 : return #"Overdue";
case 1 : return #"Today";
case 2 : return #"Tomorrow";
case 3 : return #"Upcoming";
case 4 : return #"Someday";
case 5 : return #"Completed";
//default : return [NSString stringWithFormat:#"Section no. %i",section + 1];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [CollapsableTableViewViewController titleForHeaderForSection:section];
}
On section case 2 (Tomorrow),the expected number of rows should be 3, then there are three objects in the core data store. But the result is 0. Here is the used code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int numero = [[self.fetchedResultsController sections] count];
NSLog(#"numero = %d", numero);
switch (section)
{
case 2 : return numero;
case 3 : return 30;
default : return 8;
}
}
And here is the simulator screenshot to demonstrate it (void Tomorrow section):
The problem is in your method.
At the moment you return ever the count of the number of section and not of the rows in section ;) You forgotten objectAtIndex:section
You have to use:
[[[self.fetchedResultsController sections] objectAtIndex:section] count];
enjoy!