UITableView Index without magnifying glass - iphone

I am trying to create an Indexed UITableView with out the magnifying glass icon.
I realize the UITableViewIndexSearch is creating the icon in the index but I do not know what to replace it with. Any help or suggestions would be greatly appreciated.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return index;}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
// Return the array of section index titles
NSArray *searchArray = [NSArray arrayWithObject:UITableViewIndexSearch];
return [searchArray arrayByAddingObjectsFromArray:self.fetchedResultsController.sectionIndexTitles];}

Simply replace it with nothing. Just leave the UITableViewIndexSearch constant out of your array.

Don't add UITableViewIndexSearch in sectionIndexTitlesForTableView:. Simply return the array from your fetchedResultsController.

Related

UITableView Splitting contents into Sections

I have a UITableView for which I have listed the code below.
How do I section these objects out such as the following shown below?
Math
Algebra
Geometry
Calculus
Science
Integrated 1
Integrated 2
Here is the code:
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:#"Math",#"Science",#"English",#"Social Studies",#"Spanish",#"German",#"French",#"Biology",nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
You have to use sectioned table view, that is the best way to achieve this. You can refer following tutorial for the same - http://www.xcode-tutorials.com/uitableview-–-3-sectioned-table-view/
In your UITableViewDataSource of UITableView object;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return array.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [array objectAtIndex:section];
}
will do the 'sectioning' part for your case.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [yourarray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [yourarray objectAtIndex:section];
}

how to give section alphabetical order in iphone

I have table and one global variable in appdelegate class. I store all date in this global array and showing this array value in controller in table view. I have to give section index with alphabetical order by short how to give please help me on this
I tried this code but it not working for me
Kindly help me.
h.file:
NSMutableArray *timeZonesArray;
NSMutableArray *sectionsArray;
UILocalizedIndexedCollation *collation;
.mfile
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
//return 1;
return [[collation sectionTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
//app.networkActivityIndicatorVisible = YES;
NSArray *timeZonesInSection = [app.lstAirports objectAtIndex:section];
return [timeZonesInSection count];
// return app.lstAirports.count;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *timeZonesInSection = [app.lstAirports objectAtIndex:indexPath.section];
a=(airport*)[timeZonesInSection objectAtIndex:indexPath.row];
NSLog(#"str:%#",a);
if(a!=nil){
cell.textLabel.text =a.Name;
cell.detailTextLabel.text=a.Code;
//[app.spinner stopAnimating];
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[collation sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [collation sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [collation sectionForSectionIndexTitleAtIndex:index];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
app.originAirport = (airport*)[app.lstAirports objectAtIndex:indexPath.row];
[delegate Originstart:app.originAirport forIndexPath:self.indexPathToChange];
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Set the data array and configure the section data
- (void)setTimeZonesArray:(NSMutableArray *)newDataArray {
if (newDataArray != timeZonesArray) {
[timeZonesArray release];
timeZonesArray = [newDataArray retain];
}
if (timeZonesArray == nil) {
self.sectionsArray = nil;
}
}
In order to display/arrange your data in alphabetical which in a array you have to use NSSortDescriptor
you have to make the object of this NSSortDescriptor class and give data here which you are fetching from XML
NSSortDescriptor *itemXml = [[NSSortDescriptor alloc] initWithKey:#"itemName" ascending:YES];
Now suppose you have an array sortDescriptors
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:itemXml,nil];
[yourArray sortUsingDescriptors:sortDescriptors];
I hope this will be usefull to you.
If you are looking for section-wise sorting I would hold your various sections in a dictionary that holds arrays for each of the sections and then sort each of those arrays as needed. It requires a bit more code on the table delegate methods but allows for complete control over each section's data
Another option is to encapsulate your data in a different and add a property for the section you want it in and add another sort descriptor to that so it is sorting the array by section and then by any other value. You do that by just adding another NSSortDescriptor to the array in the order you want the array to be sorted by.
The perfect example , please refer the code,
Sorting Alphabetically

[tableView numberOfRowsInSection:0 always returns 0

The data populates, but [tableView numberOfRowsInSection:0] always returns 0. There is only one section in my table. What types of things can cause this behavior?
If there is only one section always in your table, go with the following coed...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
Or in case your number of sections are depending on datasource, you can return the count of your datasource.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
Try this. It might help you.

Number of rows from tableview

I was working with the tableview and I wanted the number of rows (i.e the number of items in tableview). How can I get the number of rows, if available?
Please help me out,
thanks in advance.
take a instance variable in your header file.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
yourHeaderFileVariable = [noOfItemsInArray count];
NSLog(#"total item in table %d",yourHeaderFileVariable);
return [noOfItemsInArray count];
}
or try this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
NSLog(#"total item in table %d",[noOfItemsInArray count]);
return [noOfItemsInArray count];
}
The number of rows in table view for a section is what you are returning from (NSInteger)tableView:numberOfRowsInSection:. So if you return something like [mySource count] from this method then the number of rows is [mySource count].
For example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
// to get the count
NSInteger rowCount = [data count];
Now replace [data count] with whatever calculation you have in the delegate method.
You need to implement the delegate method :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [yourArray count];
}
You can then show the result in the table header by using the following delegate method :
- (NSString *)tableView:(UITableView *)resultTableView titleForHeaderInSection:(NSInteger)section
{
NSString *str=[NSString stringWithFormat:#"%i",[yourArray count]];
str=[str stringByAppendingString:#" matches"];
return str;
}
For example, if you have 7 results in the table, the header will display the text "7 matches"
- (NSInteger)tableView:(UITableView *)tablefirst numberOfRowsInSection:(NSInteger)section {
int count;
count=0;
count=[*your array name* count];
NSLog(#"row count=%d",count);
return [*your array name* count];
}
This function returns the number rows(items) in your tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listOfItems count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return numberOfRows;
}

how to control the index displayed on the tableview

I am able to show a index on the right side similar to songs view on the ipod. During searching the index bar gets minimized automatically. And when i go back to my actual table view the index size is small and it displays only few alphabets. How to stop the resizing of this ?
you have to place proper delegate methods of UITableView in your viewController.m file.
for example I have placed following code.
Please read comments carefully.
#pragma mark -
#pragma mark Table view data source
// an array count which has values for index - like A,B,C etc.
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//return [NSArray arrayWithArray:keys];
return [keys count];
}
// an array which has values for index - like A,B,C etc.
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return keys;
}
// return how many number of rows are required for each section.
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[dMain valueForKey:[keys objectAtIndex:section]] count];
}
// return title of section
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [keys objectAtIndex:section];
}
// create each row for different sections
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"%i %i",indexPath.row,indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.font=[UIFont fontWithName:#"Helvetica" size:12];
//cell.textLabel.text=[[[objects objectAtIndex:indexPath.row] valueForKey:#"marketname"] stringByReplacingOccurrencesOfString:#"_and_" withString:#"&"];
cell.textLabel.text=[[[[dMain valueForKey:[keys objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] valueForKey:#"marketname"] stringByReplacingOccurrencesOfString:#"_and_" withString:#"&"];
}
return cell;
}