I have a tableView where I have implemented section headers and I have the section titles appearing along the right hand side. However, when I tap the index it doesn't scroll to that section even though I can see in my logs that it is returning the correct section.
Here is my method for tableView:sectionForSectionIndexTitles:atIndex
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
NSLog(#"Title: %#", title);
NSLog(#"index: %d", index);
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
Any help would be great, thanks.
If you want to create alphabets scroll. This will helps.
-(void)viewDidLoad {
//section index in tableview
alphabetsArray = [[NSArray alloc] initWithObjects:#"#",#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return alphabetsArray;
}
Related
I've seen a number of people who have asked a similar thing, but answers to their questions are not the answers to mine.
1) I have created a single view application with an empty View Controller. In that, I dragged a new Table View (style Plain) with a single prototype cell of style Basic.
2) I am trying to learn about dynamically changing the behaviour of TableViews, so I have a mutable array called sectionRows, which will contain the number of rows per section. At the moment, a single section with a number of rows would be an achievement :)
3) In my ViewController.h I have set the delegates
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#end
I have also control-dragged from the TableView to the ViewController Yellow-Circle and set the datasource and delegate outlets.
4) In my ViewController.m, I have defined some global variables
#interface ViewController ()
{
NSMutableArray *sectionRows;
UITableView *myTableView;
}
The first is my data array (containing the number of rows per section and the second is a pointer to my TableView, which I have identified using a numeric View tag of '1'.
5) In my viewDidLoad, I initialize everything:
- (void)viewDidLoad
{
[super viewDidLoad];
myTableView = (UITableView *)[self.view viewWithTag:1];
sectionRows = [[NSMutableArray alloc] init]; // Create sectionarray
[sectionRows addObject:[NSNumber numberWithInteger:1]];
myTableView.dataSource = self;
myTableView.delegate = self;
[myTableView reloadData];
}
As you can see, I even make sure that I set the datasource and delegate again but this hasn't made any difference.
5) I have overloaded 3 methods.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Returning %# rows", [sectionRows objectAtIndex:section]);
return (NSInteger)[sectionRows objectAtIndex:section]; // the number referenced in the array...
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"Returning %li sections", sectionRows.count);
return sectionRows.count; // the size of the sectionRows array
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"xxx";
NSLog(#"Setting cell to %#", cell);
return cell;
}
Now, when I run this, I am getting NSLog returning confirmation that there is a single section and a single row:
2014-07-27 19:58:34.599 TableViewTests[12877:60b] Returning 1 sections
2014-07-27 19:58:34.600 TableViewTests[12877:60b] Returning 1 rows
However, as you can see cellForRowAtIndexPath is not being called.
None of the other things I have seen point to what I am doing wrong. I am doing what I thought I did successfully in another simple project (to learn) but I must be doing something else differently.
Any ideas what I am missing?
Thanks in advance
Jon
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Returning %# rows", [sectionRows objectAtIndex:section]);
return (NSInteger)[sectionRows objectAtIndex:section]; // the number referenced in the array...
}
This is incorrect. You cannot cast what you get from your array to an NSInteger. It's a pointer. Assuming you store NSNumbers into the array:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Returning %# rows", [sectionRows objectAtIndex:section]);
return [[sectionRows objectAtIndex:section] integerValue]; // the number referenced in the array...
}
I am new to coding and have just started on working on a new app. I have been stuck for a few days searching for answers on how to remove null headers in a table header.
This is my code at the moment:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view=[DetailGroupHeader loadInstanceFromNib];
NSDictionary *category = [self.categories objectAtIndex:section];
if ([[self.restaurant objectForKey:#"restaurant_id"] isEqual:[category objectForKey:#"restaurant_id"]]) {
DetailGroupHeader *headerView=(DetailGroupHeader *)view;
headerView.lblTitle.text=[category objectForKey:#"maincatename"];
headerView.btnReveal.indexPath=[NSIndexPath indexPathForRow:0 inSection:section];
}
return view;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.categories.count;
}
At the top of -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section I am initiating the names of the headers, and this part is working perfectly fine.
However when I get to the next function -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView, to count the number of headers I receive null values, how can I get rid of the null values in the header?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view=[DetailGroupHeader loadInstanceFromNib];
NSDictionary *category = [self.categories objectAtIndex:section];
if ([[self.restaurant objectForKey:#"restaurant_id"] isEqual:[category objectForKey:#"restaurant_id"]]) {
DetailGroupHeader *headerView = (DetailGroupHeader *)view;
NSString *maincateName = [category objectForKey:#"maincatename"];
if ([maincateName isEqual:[NSNull null]]) {
maincateName = #"";
}
headerView.lblTitle.text = maincateName;
headerView.btnReveal.indexPath = [NSIndexPath indexPathForRow:0 inSection:section];
}
return view;
}
I believe you are making some really basic mistakes. As I can see you are only showing those rows which have [[self.restaurant objectForKey:#"restaurant_id"] isEqual:[category objectForKey:#"restaurant_id"]] condition true. But you are returning numberOfSectionsInTableView, count of complete categories array as self.categories.count
I will suggest you to check:
Calculate count of header where condition [[self.restaurant objectForKey:#"restaurant_id"] isEqual:[category objectForKey:#"restaurant_id"]] satisfies
Check if the condition you are working with really have it's existence, (print log or set break point)
That's all I can say for now. If problem persist, add log print of your self.restaurant dictionary and self.categories array.
I got a table view with approx 45k items. I want to use the indexbar to be able to scroll faster through my list.
Now I've looked at other questions on here, but I just can't figure out what to do.
I've got the following two functions:
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return alphabetsArray;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return index;
}
I don't understand what I should do next. Like, I have an array called airports which is sorted alphabetically. What should I be doing with this array?
update
so I found an article I could use and I'm pretty close now, I think. My table view only shows 2 items now though.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"ROWS METHOD CALLED");
NSArray *unsortedKeys = [alphabetizedAirports allKeys];
NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSString *key = [sortedKeys objectAtIndex:section];
NSArray *airportsForSection = [alphabetizedAirports objectForKey:key];
return [airportsForSection count];
}
I have about 60 items in my sortedKeys array. The airportsForSection array only gets 2 items though so when it returns the array, the tableview only shows 2 items. Any ideas how to solve?
I have just started in ios, and I am stuck with UITableView.
I want to implement index bar in right side of the view. I have implemented this but when I tap on the section on the index bar it's not working.
This is my code. Here indexArray is as "A-Z" element and finalArray is my UITableView mutable array.
Please tell me what should i implement in sectionForSectionIndexTitle.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [finalArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
return [finalArray objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return indexArray;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle: (NSString *)title atIndex:(NSInteger)index
{
}
This is perhaps one of the most confusing methods in the UITableViewDataSource, its best to consider an example:
Lets say you have a table view with 4 items:
section index - 0 section title - 'A'
items: ['Apple', 'Ant']
section index - 1 section title - 'C'
items: ['Car', 'Cat']
You then have 26 section index titles (the index on the right side of the table view): A - Z
Now the user taps on letter 'C' you get a call to
tableView:sectionForSectionIndexTitle:#"C" atIndex:2
You need to return 1 - the section index for the 'C' section.
If the user taps 'Z' you will also have to return 1 since you only have 2 sections and C is the closest to Z.
In the simple case where you have the same sections in your table view as the index on the right had side its ok to do this:
- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index
{
return index;
}
Otherwise it depends on your setup. You might have to look for the title in you section titles:
NSInteger section = [_sectionTitles indexOfObject:title];
if (section == NSNotFound) // Handle missing section (e.g. return the nearest section to it?)
I am trying to index my UITableView with following code
In my ViewDidLoad
arrayOfTableView = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20",#"21",#"22",#"23",#"24",#"25",#"26", nil];
And in my sectionIndexTitlesForTableView Method
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *tempArray = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20",#"21",#"22",#"23",#"24",#"25",#"26", nil];
return tempArray;
}
And in my sectionForSectionIndexTitle Method
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
It's appear IndexBar beside of my tableview . However when i tap on IndexBar , it's doesn't scorll to there.
I have already test with NSLog, that event or not? It's event.
But don't scroll to tableView.
please help me to find out.
I think you need to check your numberOfSectionsInTableView: method.
Also you should implement this method (tableView:sectionForSectionIndexTitle:atIndex:) only for table views with a section index list—which can only be table views created in the plain style (UITableViewStylePlain).