Pass NSInteger to a tableView controller - uitableview

Hi
I'd like to pass an NSInteger to a tableView controller and then use it to set a badge.
In this case, the NSInteger I'd like to pass is the number of rows in a tableView, returned with Core Data (numberOfRows).
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger numberOfRows = 0;
if ([[fetchedResultsController sections] count] > 0) {
id NSFetchedResultsSectionInfo sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
numberOfRows = [sectionInfo numberOfObjects];
}
return numberOfRows;
}
How can I declare another NSInteger and then pass it to a tableView controller?
Thanks,
Matthew

It sounds like you may need two fetched results controllers in the second table view controller. The first FRC would manage the table itself and the second would calculate the existing number of rows in the first table's data.
You would have to assign the second tableview controller as the delegate for both FRC and then in the delegate methods test which controller fired a change and take the appropriate action.

Related

Updating Core Data executes UITableView problems [duplicate]

I have a problem with UITableView sections. I'm using NSFetchedResultsController to populate table and I've provided sectionNameKeyPath: on initialization.
Basiclly I have a table view with one section but when user taps on cell it is changing one core data attribute and should create second section. I think I've accomplished this but...
When I tap on the first cell it creates new section above (this is how it should be) but when my first tap is on another cell it creates section at the botton of table view. You can preview this on screenshots below:
Here are some Table View delegate methods from my app:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[_fetchedResultsController sections] count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[_fetchedResultsController sections] objectAtIndex:section] name];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
And my question is: How to manually provide section order?
For this example solution was pretty straight - adding one NSSortDescriptor to existing one in NSFetchedResultsController sort descriptors array.
So now, fetched data is firstly sorted by section and then it is sorted alphabetically.

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
}

Setting sectioned table header using nsfetchedresults controller

I am using NSFetchedResultsController in my project along with core data. I am able to fetch all the objects from the database. I am displaying them in a sectioned table view. I want to display the total number of rows in each section as the header of the section table view. I am not getting any kind of help for this. I have tried a lot but don't know how exactly to set it . Any kind of help is appreciable.
Something like the following should work:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSInteger numberOfRows = [[[self.frc sections] objectAtIndex:section] numberOfObjects];
UITableViewHeaderFooterView *sectionHeaderView = [[UITableViewHeaderFooterView alloc] init];
sectionHeaderView.textLabel.text = [NSString stringWithFormat:#"Total objects %d", numberOfRows];
return sectionHeaderView;
}
The trick is getting the right section out of the NSFetchedResultsController and calling the numberOfObjects method on it. The sections in an NSFetchedResultsController are proxy objects that conform to the NSFetchedResultsSectionInfo delegate.

numberOfSectionsInTableView for two UITableViews

I have two UITableView which appears according to UISegmentControl.selectedSegmentIndex. The first table is grouped style and the second is plain.
NSFetchedResultsController is for the first UITableView, and NSMutableArray for the second.
In my numberOfSectionsInTableView:
return [_fetchedResultsController.sections count];
In this case the second UITableView's data displays for 3 times (because FirstTableView section count = 3).
I would like for the second tableView numberOfSectionsInTableView
return 1;
You should check which tableview you are displaying before returning the count. You can use the tableView input param for this.
For eg:-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == self.firstTableView) //or (tableView == firstTableView)
return [_fetchedResultsController.sections count];
else
return 1;
}
You can use the following condition...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if(tableView == firstTableView)
return 1;
else if (tableView == secondTableView)
return 2;
//... and so on you can do like this....
}
All the best !!!
I guess you are setting the same data source for both table views. If that is the case, then in your:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if(tableView == firstTableView){ //assuming you have a reference to the first table view
return [_fetchedResultsController.sections count];
}else{
return 1;
}
}
You will have to do this in all your datasource methods. Instead it would be easier to make the datasource of the tableviews different.
To do this you can drag an object (the blue cube ) from the object library and set its class as say, SecondTableDatasource. And then set this object as the datasource for your second table, and implement the corresponding datasource methods in that class.
You can add tag for each tableView and check for each tag in numberOfSectionsInTableView

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