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
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];
}
I'm using the following section of code - it should be simple, but it's causing me a lot of trouble:
NSUInteger index[] = {1,0}; // top row of section one
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath
indexPathWithIndexes:index length:2]] withRowAnimation:UITableViewRowAnimationNone];
The code crashes on the second line, with the error that "The number of rows in section zero is not equal to the number of rows in section zero before the update".
This error is true, I'm changing the number of rows in section zero. But surely this shouldn't effect whether or not I can reload a row in section 1!?
-
Am I misunderstanding how something works, or is something else going wrong somewhere? Any help or ideas are much appreciated :)
I think you also need to update the numberOfRowsInSection.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return rowCount;
}
If you are changing (reloading) the number of rows in section 0 (probably by deleting or adding some rows in it) then -
(NSInteger)tableView:(UITableView
*)tableView numberOfRowsInSection:(NSInteger)section
is called to get the new number of rows. Here you need to return the correct number of rows i.e. one row less, if you deleted a row in section zero.
So basically, if you were returning [someArray count] for number of rows in section 0, you need to update this someArray as well.
I want an alphabet strip (like in addressBook) on UITableViewController. Please help.
You need to implement the TableView delegate method
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}
Cheers
I think the correct answer is
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView {
}
just for the record!
Apple documentation:
Return Value
An array of strings that serve as the title of sections in the table view and appear in the index list on the right side of the table view. The table view must be in the plain style (UITableViewStylePlain). For example, for an alphabetized list, you could return an array containing strings “A” through “Z”.
I am implementing a table index view and amazed to see how my table indexes are working even without implementing:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index method.
I have only implemented:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
Strangely, when I am running in breakpoints, Once i click on any of the index values my
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method is getting called.
Any clue why this is so happening and what is the significance of sectionForSectionIndexTitle method then.
if you have a list of all letters in alphabet and your list only contains some entries, you could use the following code:
//Asks the data source to return the index of the section having the given title and section title index.
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
if (tableView == self.searchDisplayController.searchResultsTableView || self.searchBar.text.length > 0)
{
return 0;
}
else
{
//direct - firsttime match
if([self.realMIndexArray containsObject:title]) {
NSInteger count = 0;
for(NSString *character in self.realMIndexArray)
{
if([character isEqualToString:title]){
return count;
}
count ++;
}
}
else {
//take next higher letter from alphabet and check if its contained in the "available letters list"
//if not, select last entry of list
for(int i = [self.indexArray indexOfObject:title] + 1; i < [self.indexArray count]; i++) {
NSString* character = [self.indexArray objectAtIndex:i];
if([self.realMIndexArray containsObject:character]) {
return [self.realMIndexArray indexOfObject:character];
}
}
return [self.realMIndexArray count] - 1;
}
return 0;// in case of some eror donot crash d application
}
}
realMIndexArray count == letters really existing in list
indexArray = list of all letters in alphbeth.
hope this helps someone (took me a little bit of time to figure it out)
Any clue why this is so happening
Yes. When you tap (you do not click on an iPhone) on a table's index, the underlying table view will want to jump to that section, and the cells in that section. In order to do that, it has to ask the data source for those cells so it can render them on the screen.
what is the significance of
sectionForSectionIndexTitle method
then.
The documentation for tableView:sectionForSectionIndexTitle:atIndex: (which is an optional method in the UITableViewDataSource protocol) says:
Asks the data source to return the
index of the section having the given
title and section title index.
and
You implement this method only for
table views with a section index
list—which can only be table views
created in the plain style
(UITableViewStylePlain).
Does this apply for your UITableView? In other words, are you using a grouped table view style?
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;
}