issue with numberOfSectionsInTableView method - ios

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"Test1");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
NSLog(#"No. of sections- %d",section);
return [[[BNRItemStore sharedStore] allItems] count] ;//no. of rows
}
In the above scenario, numberOfSectionsInTableView: method is being called twice before tableView method is called. I can't understand why.
The other thing that confuses me is that when I returned 1 as number of sections in numberOfSectionsInTableView: method, why does it not reflect in - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method? It rather logs it as 0.
Doesn't the section parameter get updated since I set it as 1 previously? I mean at runtime the tableView:numberOfRowsInSection: method is called internally so obviously section parameter is supposed to hold some value.
Isn't that the case?

Number of section is the count of sections in the TableView & the index of row & section starts from 0.

Sections and rows in tableViews are just like arrays, they start with 0.

Try like this inspite of returning 1:-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"Test1");
return [yourArray count];
}

Related

UITableView showing inconsistent number of rows compared to data source

I'm trying to make a UITableView present a determined number of rows for a section, but even when I verify that its data source is returning x number of rows for numberOfRowsInSection, the table view shows x-1.
The exception to this unexpected behavior is if the numberOfRowsInSection is less than 3.
I've even put a breakpoint in cellForRowAtIndexPath and I confirmed it's being called for the row that is not appearing.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == SectionNumber_One) {
return 6;
} else {
return self.numberOfProjectRows; // This returns x, but x-1 rows are being shown
}
}
For example, if self.numberOfProjectRows is 5, only 4 rows are shown for the second section.
If I increase it manually to 6, it shows 5 rows but the data that should be in the 5th position, isn't there.
It doesn't seem to be related to screen size as I tested it on an iPad with same results.
Why is this happening? Is there some other possible modifier of the number of rows in a section?
I'm attaching an screenshot if it's of any help.
EDIT - Here are my delegate methods:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Cell with reuse identifier setup
if (section == SectionNumber_One) {
// cell setup for section one that's showing up ok
} else if (section == SectionNumber_Two) {
UITextField *projectField = cell.projectTextField;
if ([self.userProjectKeys count] > row) {
projectField.text = self.availableProjects[self.userProjectKeys[row]];
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Hide the password row for existing users
if (indexPath.row == FieldTag_Password && ![self.user.key vol_isStringEmpty]) {
return 0.0f;
} else {
return UITableViewAutomaticDimension;
}
}
The problem is probably not in your Datasource methods, but your Delegate methods, tableView(_:heightForRowAt:).
If you do not return a correct height for the cell, it won't show up.
It doesn't matter if you write 1000 cells in your datasource. If you don't return the height, they wont show up.
You are not comforming to the MVC pattern in implementing the table. You must return the count of the tableView's datasource, not variables of it.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == SectionNumber_One) {
return 6;
} else {
return [_displayingArray count]; // <-- here
}
}
If you want different items for each sections, then declare different datasource (ie array) for each of them.
EDIT: Even returning constant 6 is dangerous in the other section - you ought to add items to another fixed array and return that array's count in this delegate.

NumberOfRowsInSection return issue

I am populating my UITableView with data from an RSS feed however I don't want to return all the values. how do you set the number of return values in the method below ?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return itemsToDisplay.count;
}
The method tableView:numberOfRowsInSection: is a callback, this is where you specify the number of rows to display.
Trivial answer:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return MIN(itemsToDisplay.count, 10);
}
It is necessary to insure that the returned count is no greater than the number of items in itemsToDisplay.
This will cause a maximum of the first 10 to be displayed. Is there more to the question?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
u can just pass any number
int returnedvalue = 5;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return returnedvalue;
}

How to controll number of rows in section in tableView?

How can I set number Of rows in section in SUBCLASS of UITableView (not in view controller)?
is there a function , say, [self setNumberOfRowInSection:] or something like this?
I know about delegate method
- (int)tableView:numberOfRowsInSection;
BUT I'AM NOT IN VIEW CONTROLLER!
A made a class, wich is subclass of UITableView, and in some action, a want to SET (not to GET)
number of rows for section.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
this will create 5 rows in section so you can write whatever your desired output is if from array then write [myArray count]; instead of 5.
You can use Delegate& DataSource method of UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10; //no. of row you want
}

use indexpath.setion for a table with different section number

I have two tableviews and cells are configured depending on which table view it is, they are implemented in the cellForRowAtIndexPath method via indexpath.section. However, the two tableviews have a different number of sections. Is there a way to go about this issue? Thanks
When i said they had different section numbers i meant the following:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if(tableView==byTeam) {
return [teamnameswithoutRepeat count];
} else {
return [JSONPointsArray count];
}
// Return the number of sections.
}
When i use the following in cellforrowatindexpath:
label.text=[currentarray objectAtIndex: indexpath.section];
there is a range error because indexpath.section is too big, it is using the JSONPointsArray to determine the number of sections, however when i run the app the by team tableview has the correct amount of sections.
Your cellForRow... method needs to be similar:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == byTeam) {
// process the "byTeam" table using the teamnamesiwthoutRepeat array
} else {
// process the other table using the JSONPointsArray array
}
}

UITableView Section with Index

Anything I missed in the following code? I can create a list but the list is array x26 times (26x section) in the UITableView. A list shown, but the section title is only point to 'A'
arr_indexpeople > An array of A,B,C,D,E.....Z
arr_completeArray > An array of a list of people
Thank you.
return [arr_indexpeople objectAtIndex:section]
You are returning objectAtIndex 0 in titleForHeaderInSection. Change it to-
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [arr_indexpeople objectAtIndex:section]
}
The datasource method will get called for every section to load, so give the dynamic index of a section as input variable for arr_indexpeople as shown below.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [arr_indexpeople objectAtIndex:section]
}

Resources