I am having a problem with a semantic issue I am getting in my MasterViewController.m. The issue I am getting is this:
MasterViewController.m:89:1: Control may reach end of non-void function
And the same error on this line:
MasterViewController.m:105:1: Control may reach end of non-void function
It is happening at the end of both of my switch statements, but I am not exactly sure why. All I am trying to do is make sections in my MasterViewController.m.
I am two days new into Objective C, so it could be a really simple problem I am simply looking over because I am not familiar with all of the errors yet. So if anyone could point me in the right direction, it wold be appreciated.
My Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section)
{
case 0:
return [_section1Items count];
break;
case 1:
return [_section2Items count];
break;
default:
break;
}
}
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch (section)
{
case 0:
return #"This is section 1";
break;
case 1:
return #"This is section 2";
default:
break;
}
}
Thanks
It is possible that your methods don't return anything but they are supposed to return an Integer (NSInteger) and the reference to an NSString.
Just add some meaningful return statement to your default branch. e.g. return 0 for the first method and return nil or return #"" for the second.
Related
All,
I have a grouped UITableView with a possible total of 3 sections. There could be 1, 2 or 3.
My issue is that for each section I use a different header & footer view. I am choosing which header/footer to show by checking the section #.
This obviously does not work, as section 0 does not always represent what 'header' 0 shows.
Example:
Header #0 = "Game in progress". But no games in progress are returned from the database. Only 'Games Ended" exist. Therefore section 0 would be all 'games ended'. I don't want 'Games Ended' to use the 'Games in Progress' header.
I can't find a way to check the section value, and not the number.
To put it simply, I would like to be able to show section header #3 for section name #3, even if section name #3 is section #0.
I know this seems trivial, and is probably simple... but I am stuck. Any help is appreciated.
Thanks.
----- CODE -----
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[fetchedResultsController_ sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController_ sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 0)
{
return 50.0f;
}
else if (section == 1)
return 50.0f;
else
return 50.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if(section == 0 )
{
return 50.0f;
}
else if (section == 1)
return 5.0f;
else
return 80.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(section == 0)
{
return headerView1;
}
else if (section == 1)
return headerView2;
else
return headerView3;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if(section == 0)
{
return footerView1;
}
else if (section == 1)
return footerView2;
else
return footerView3;
}
Obviously, deciding which header / footer to show by checking the section # is wrong (this is bad MVC). For a solution to your problem, it would be better to see some actual code, although I think I can suggest something in general:
The sections that you show are taken out of some data source - an array, a dictionary or some other collection (this is the same collection you use to determine, for example, the return value for the numberOfSectionsInTableView: delegate method. If you haven't done so already, you should incorporate these data instances into some object that contains the data itself (this is the data that you normally need for displaying the cell/header/footer elements along with the actual data values) - In this object, add an additional "HeaderType" enumerated value, so that each object "knows" how it is supposed to be displayed. This way your MVC is perfect: You have your data stored in a collection of custom objects, your controller knows how to display the data by it's type and of course your view shows the data properly based on the controller's instructions.
Here is an example of an enumeration that could help:
typedef enum {
kHeaderTypeGameProgress,
kHeaderTypeGameStats,
kHeaderTypeGameDate
} HeaderType;
In your "viewForHeader" or "viewForFooter" methods, just add a switch type to check the data's HeaderType and create a view accordingly. Hope I helped, good luck!
It seems that in your cellForRowAtIndexPath, you must already have some logic that decides what group to show data from, maybe something like:
NSArray *group;
int section = indexPath.section;
if (![gamesInProgress count]) section++;
switch (section) {
case 0:
group = gamesInProgress;
break;
case 1:
group = finishedGames;
break;
// etc.
}
In your viewForHeaderInSection, write similar code that sets a NSString instead of NSArray.
I have four sections in my UITableView, how do I add a header for each section?
I am using the following code, but it's not working.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0)
return #"Tasks";
if (section == 1)
return #"Appointments";
if (section == 2)
return #"Activities";
if (section == 3)
return #"Inactivities";
}
Are you sure that you've implemented tableView:heightForHeaderInSection:?
In your case the titles were actually set but the headers' heights were 0 by default. Add this UITableViewDelegate method if you haven't yet:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40; //play around with this value
}
P.S.: Consider using switch statement in such methods
If your section Header height is constant for each section then you do'nt need to implement heightForHeaderInSection delegate method, And you could use as below
myTableView.sectionHeaderHeight = 40 ;
In Apple Documentation
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
Change your if statements like this
if(section == 0)
return #"ABC";
else if (section == 1)
return #"XYZ";
else
return #"PQR"
I hope this helps. Please let me know if I can be of any more help.
Pinku:)
I am trying to get new cells with data from a NSManagedObject to a tableview with a fixed section.
The first section is a fixed section. The second section is where the user can add new data. Displaying the fixed section separate is no problem, as well as showing the dynamic content separate. But when I combine them, I get stuck with this method (see below at the 'rows = ???'). Normally you can fix this easily by doing [array count] but my ManagedObjects are not collected in an array.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
//return [sectionInfo numberOfObjects];
NSInteger rows = 0;
switch (section) {
case FIXED_SECTION:
rows = 4;
break;
case LIST_SECTION:
{
rows = ???;
break;
}
default:
break;
}
return rows;
}
Either collect the NSManagedObjects in an array (probably an instance variable of the view controller subclass you’re using) or maintain a counter that you increment when adding a cell and decrement when you remove a cell.
The NSFetchedResultsController is what you need, the manual section explains pretty much as you need it
I've got a table view with many sections, the title for these sections is just A-Z and # just like in the iPhone address book App. I have implemented sectionIndexTitlesForTableView to have the quick move to particular letter and basically just return an array of Letters A - Z and #.
This would work if my list always contains an item for every letter of the alphabet but it won't and this screws up the section index titles because hitting C in the list might go to D if the 3th section is D (ie if there is nothing in section C).
I know I could return the array in sectionIndexTitlesForTableView with only the letters that are sections but this would look a bit odd and not the same functionality as the iPhone Address book app.
How can I rectify this?
I don't see how #Rudiger's method would work if you have only have sections A, C, F, S, T and section index titles for A-Z. Such a situation might arise when using MPMediaQuery.
To get around this I've implemented this method as follows, the tableview will scroll to the correct section or the next if the one you are looking for doesn't exist.
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
NSString *sectionTitle = nil;
NSComparisonResult result;
int i;
for(i = 0; i < tableView.numberOfSections; i++)
{
sectionTitle = [self tableView:tableView titleForHeaderInSection:i];
result = [title compare:sectionTitle];
if(result != NSOrderedDescending)
break;
}
return (MIN (i, (tableView.numberOfSections - 1)));
}
UPDATE
Changed the return value to fix the situation described by Eric D'Souza.
Basically you have to implement:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [sections indexOfObject:title];
}
and return based on the index and title what section it should be on. Where sections is the array storing the list of sections
I keep getting 'warning: control reaches end of non-void function' with this code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section ==0)
{
return [comparativeList count];
}
if (section==1)
{
return [generalList count];
}
if (section==2)
{
return [contactList count];
How can I get rid of this warning?
Thanks.
Add a return 0; at the end of your method. It's basically a failsafe, if none of the if conditions are met.
If you want to make sure one of the conditions is met, return -1; should cause the application to throw an exception and crash, which might help you track down errors.
You might also look at modifying this method and replacing the if statements with a switch-case tree. With a switch-case tree, you can very easily add new sections and rearrange their order in a UITableView. The code becomes trivially easy to read by using sensible naming conventions.
It's really easy; Fraser Speirs has a good explanation on how to set this up.
One option:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section ==0) {
return [comparativeList count];
} else if (section==1) {
return [generalList count];
}
// section == 2
return [contactList count];
}