I get list of names from web service in table view. My requirement is that I need to display the names starting from alphabet A / B / C and so on in iOS. Please refer image (red rectangle) below. Any suggestions how to implement this?
Take a look at the UITableViewDataSource methods
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
you need to return an array with the titles for the various sections that will appear on the right. For instance:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return #[#"A",#"B",#"C"];
}
and
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
return the index of the section to scroll to once the index on the right is tapped. You have to handle with sorting yourself, all the indexes do is scrolling to a certain section when an index on the right is tapped.
Your friend here is UILocalizedIndexedCollation. There's an explanation of how to use it at NSHipster.
Related
I'm using a TableViewController with custom TableViweCell to gather user information. inside my custom TableViweCell there are more than 5 textfields. (NOTE : I'm using tableview controller because it helps scroll above keyboard automatically when tap on uitextfield). so when I tap on first uitextfield, automatically focus on it and automatically scroll it to up(stop near the navigation bar bottom line.). then when I tap on another uitextfield it scrolls to up with passing the navigation bar.then user has to scroll down to type on it. what is the problem with this.
should I use different custom uitableview cells for each textfield
tableview .m file implementation.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HoteluserdetailformTableViewCell *formcell = [tableView dequeueReusableCellWithIdentifier:#"hotelformcell" forIndexPath:indexPath];
return formcell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return screenSize.height;
}
Found the solution for this.if we want to use more than one text field we have to return that much of rows in number of rows in section method.
ex : if you want 5 text fields
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
note : use 5 text fields inside the same custom cell does not work for this.
First of all this is not a duplicate as I have read through very similarly titled questions but they do not give me the correct answer!
So what I have done is:
First of all create a Table View with 13 unique (contain different buttons) prototype cells where the table views content is "Dynamic Prototypes". I then created a UITableViewCell class called blurCell.
I can see the new/second UITableView but there are no cells within it?
I currently have (for the first UITableView) the code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 25;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
}
But I did this because I was using a .xib for this table.
Do I need this kind of code for the new/second UITableView as I have done everything manually in the storyboard?
Please help?
In the ViewController.m (ImagesTableViewController) that contains these two tables I have the code:
#interface ImagesTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
Which is linked to both tables
Each tableView should be declared as its own property with the delegate/data source set to self most likely in viewDidLoad
You need to do something like this for the delegate/data source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tableView==firstTableView) return 25;
else if(tableView==secondTableView) return 2;
else return 15;
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if(tableView==firstTableView) return 1;
else if(tableView==secondTableView) return 2;
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{...}
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?)
Anything I missed in the following code? I can create a list but the list is array x26 times (26x section) in the UITableView. A list shown, but the section title is only point to 'A'
arr_indexpeople > An array of A,B,C,D,E.....Z
arr_completeArray > An array of a list of people
Thank you.
return [arr_indexpeople objectAtIndex:section]
You are returning objectAtIndex 0 in titleForHeaderInSection. Change it to-
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [arr_indexpeople objectAtIndex:section]
}
The datasource method will get called for every section to load, so give the dynamic index of a section as input variable for arr_indexpeople as shown below.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [arr_indexpeople objectAtIndex:section]
}
Is it possible to create an index (using UILocalizedIndexedCollation or otherwise) other than A-Z for a table view, e.g., change the A-Z vertical bar on the right of a table view to 1st, 2nd, 3rd.
Yes it is possible..
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
is method where you declaring what to show on the section index. i.e. on the right of the table.
and
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
is the method where you handling touch events.. so you can match with the section title you have given in first method and use it to make decisions.