NumberOfRowsInSection return issue - ios

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;
}

Related

clang-formatter: Prevent new line in function declaration

In Objective-C how to prevent new lines here:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
to see this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
Tried this, but apparently not works with Objective-C:
AllowAllParametersOfDeclarationOnNextLine: false

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.

issue with numberOfSectionsInTableView method

- (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];
}

UITableView & Controller gives me empty table view

I have an application (Navigation Controller) in which the second controller is a Table View Controller. I am able to send my required data through the segue, but unable to update the entries in the tableview programmatically.
I'm new to IOS.
This is my header file
#import <UIKit/UIKit.h>
#interface HCIResultListViewController : UITableViewController
#property (strong,nonatomic) NSMutableDictionary *resultList;
#property (strong, nonatomic) IBOutlet UITableView *tableListView;
#end
This is my implementation file.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"HelloCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Hello";
// Configure the cell...
return cell;
}
Can anyone tell me how to work with tableview & Controllers?
I did go through several documentation and examples but really couldnt understand
You need to specify the rows and section count for the table view. like this.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [sourceData Count]//i.e., 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"HelloCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Hello";
// Configure the cell...
return cell;
}
What would you expect in your UITableView if you set the numberOfRowsInSection to 0?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 0; // it should be size of your Array or List
}
You should change the number of rows to the Array size or whatever data you are populating within the UITableView
you need to return at least 1 to be able to see some data
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Number of sections of tableView is 0! make it 1
It may help you
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 4;
}
replace your method this :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
return 1;
}
That ist exactly what Apple's template meant by stating:
#warning Potentially incomplete method implementation.
You need to return appropriate values there.
Return 1 from numberOfSectionsInTableView if you just have 1 section (that ist the normal case I would say).
And return the number of rows that you want to be filled from numberOfRowsInSection. That may well be the count of an array or the number of fetched data sets or some hard coded value/constant value if it is not dynamic.
cellForRowAtIndexPath will then be called as maximum for each row/section combination that is possible but only for those cells that are visible from start or are about so be scrolled into visible areas.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//warning Incomplete method implementation.
// Return the number of rows in the section.
//
return 1;
}
As per this Datasource Method say that as number row require and you have to return 0 so not generate any row so also not generate cell so as per our requirement static otherwise dynamic generate the row at least 1 row return.

iOS - Add a cell to empty UITableView Section w/ Core Data and NSFetchedResultsController

I'm using Core Data and an NSFetchedResults controller to populate a UITableViewCell (plain style) in my app. The TableView has 3 sections, each with a viewForHeaderInSection and a viewForFooterInSection. The user will be entering items that will be put in each section.
When one of the sections has no items in it, it completely disappears. I'm wondering if it would be possible to make it so if a section is empty, it would display a new cell that would say "no entries" or something like that (maybe with a UIImageView or another view?), which would then go away if a new item was put into that section.
Does anyone know how to accomplish this?
Here's the code for my data source.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}
Also, the section itself gets removed from the TableView when it has no rows, so how could I make it so the "no entries" cell gets added to the correct section?
If the section don't appear in the table view, it isn't returning from the fetch results, despite the fact you said you have 3 sections.
You can return it manually by:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
Then
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects] == 0){
return 1;
} else {
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
}
In your cellForRowAtIndexPath: test for empty..
if ([[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects] == 0){
cell.textLabel = #"No Entries";
}
Just a idea.
EDIT: Be careful, if you try to catch a index that isn't returned by the fetch, your app can crash.
You need to modify your numberOfRowsInSection function to check if there are no real rows for that section, and return 1 if that is the case (instead of 0).
Then, in the cellForRowAtIndexPath, check if you have data for that section, and just create your "no entries" cell when you see that you don't.

Resources