If i needed more rows in one section than another, is there a code i could implement to do this?
I have 3 sections altogether but in one section i require 6 rows, in another only 3, and in the last perhaps 3 again.
Regards,
Jon
You just implement the method in the title that returns a different number for different section.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
return 6;
else
return 3;
}
Related
How would I be able to customize the amount of cells in each tableview group? Basically, I need 2 cells in the first group, 4 cells in the second group, and one cell in the third. I can't seem to find anything, which is odd, unless I'm calling it the wrong thing. Thanks in advance. :)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
return 2;
else if (section == 1)
return 4;
else if (section == 2)
return 1;
}
Number of cells in each table section is specified in table's data source tableView:numberOfRowsInSection: method. Somewhat dummy example for your case:
- (int) numberOfSectionsInTableView:(UITableView*)tableView{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
int numbers[] = {2,4,1};
return numbers[section];
}
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'm creating a new subclass of UITableViewController, and with it the below default
implementation.
It doesn't compile , cause clearly there is no variable called "number of sections",
what's going on here ?
The error is : "expected expression before '<' token"
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return number of sections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return <#number of rows in section#>;
}
The strings number of sections and <#number of rows in section#> need to be actual integers, corresponding with the number of sections and rows in your data source.
For example, if you have an array of five objects that you want to represent with a table view, and you want them all to go into one section, you need to return 1 from -numberOfSectionsInTableView: and 5 from -tableView:numberOfRowsInSection:.
You may want to read Apple's Table View Programming Guide for iOS to get some familiarity with how table views work, before writing any code.
In
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
The default Return Value is 1.
Check it in documentation
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Is a required method
I would like to use and display two tables on a UI view. Please let me know how to do this. Any code same will also be appreciated.
Thanks,
Sandeep
Add 2 UITableViews to your view in IB and connect them to 2 different outlets in file owner (or simply assign different tag properties).
Set delegate and data source for them (may be same view controller for both).
In delegate/data source methods you do the following:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == myFirstTable)
// return value for 1st table
if (tableView == mySecondTable)
// return value for 2nd table
return 0;
}
or if you use tag approach:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
switch(tableView.tag){
case firstTag:
// return value for 1st table
case secondTag:
// return value for 2nd table
}
return 0;
}