How to search in UITableview which have multi section in iOS - ios

My UITableView have 2 section are "Local currency" and "All currencies".
No problem when I search and both section return results. But when I search and return results only within section 2 then no cell in section "All currencies" be displayed. It 's just only display header of "Local currency" with 0 cell.
I want to when I search if which section no have in result then this header of its session will be hidden and just which secion have result will be display header and its content.
This is my code and searchResultsLocal,searchResultsPopular are the two results NSArray when I search:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//if (searchResultsLocal.count > 0 && searchResultsPopular.count > 0) {
// return 2;
//}
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return [searchResultsLocal count];
}
return searchResultsPopular.count;
}
Ay help is appreciable. Thanks in advance.

Tks u so much.I have figure out solution :
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
return 0;
}
return 20;
}

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.

Index Section in viewForHeaderInSection

i have UITableView header custom like Instagram where my section acts header, in this tableview i also using paging to get the data from server.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (currentPage == 0) {
return 1;
}
if (currentPage < totalPages) {
return myObject.count+1;
}
return myObject.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection (NSInteger)section
{
myTimeline = [myObject objectAtIndex:section];
//the rest of my code to display text in UILabel cell, etc
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (cell.tag == kLoadingCellTag) {
currentPage++;
[self getTimeline];
}
}
Now the problem is, I already make sure that my paging is working and myObject array count is increase.
But when i do the scroll it crash in method viewForHeaderInSection with reason "index 10 beyond bounds [0 .. 9]'" (i set the paging in in server with 10 items per page)
I think the section in ViewForHeaderInSection not updating with numberOfSectionsInTableView method that i already setup.
So how to make the section in ViewForHeaderInSection updated?
Thanks..
Your numberOfSectionsInTableview has return myObject.count+1;, so the table view is dutifully asking for the header for the last section, which is beyond the bounds of your myObject array. You need either to remove the +1 or, if it's there for a reason, to include code in viewForHeaderInSection to handle that special case:
if (currentPage > 0 && currentPage < totalPages && section == myObject.count) {
// create whatever header view is appropriate for the final section
} else {
myTimeline = ... etc
}

ios: uitableview - how to remove first section

I have several sections in a uitableview as shown here:
How do I hide the first section header e.g. Men.
Try overriding GetHeightForHeader in UITableViewSource, and returning 0 it's the first section
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0) return 0;
return tableView.sectionHeaderHeight;
}
In Xamarin, it would look like:
public override float GetHeightForHeader (UITableView tableView, int section)
{
if (section == 0)
return 0;
return tableView.SectionHeaderHeight;
}

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.

allow editing in uitableview, but not in one row

I have a UITableView with multiple sections. When I set the table into edit mode, then I would prefer that one section (which only includes 1 row) does not display the edit symbol and not call tableView:commitEditingStyle:forRowAtIndexPath:.
Is this possible?
regards
John
here i assume for section 1 and row 0 which can not edit
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
if (indexPath.section == 1) {
if (indexPath.row == 0) {
return NO;
}
}
return YES;
}

Resources