I want an alphabet strip (like in addressBook) on UITableViewController. Please help.
You need to implement the TableView delegate method
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}
Cheers
I think the correct answer is
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView {
}
just for the record!
Apple documentation:
Return Value
An array of strings that serve as the title of sections in the table view and appear in the index list on the right side of the table view. The table view must be in the plain style (UITableViewStylePlain). For example, for an alphabetized list, you could return an array containing strings āAā through āZā.
Related
Here i need to hide the phone number, email , birthDate, anniversary date and other labels in case there is no values for those fields. How can i do this?
Many ways, starting with the simplest:
self.emailLabel.hidden = YES;
But you probably want to reformat the other parts of the view to fit the empty space. Keeping it simple, you would then do something like this:
self.phoneLabel.frame = CGRectOffset(self.phoneLabel.frame, 0, -self.emailLabel.bounds.size.height);
... and so on for anything below. But you can see how this would become tedious. The next and probably best alternative is a UITableView that adjusts it's section count based on whether some of that data is present. That would go like this. Prepare a mutable array of arrays with the parts of your model and their values.
- (void)prepareModel {
self.model = [NSMutableArray array];
[self.model addObject:#[#"Name", #"Judy"]; // get "Judy" from your data
if (/* model has email */) {
[self.model addObject:#[#"Email", #"judy#gmail.com"]; // get email from your model
}
// and so on for conditional parts of your model
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.model.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
return self.model[section][0];
}
And the cellForRow would init the cell using self.model[section][1].
What you can do is simply hide the UILabel's if the value for the NSStrings that you are putting them in is NULL/nil .
NSString *labelString;
if([labelString length]>0)
{
}
else
Label.hidden = YES;
It is probably a better idea to use a UITableView to do this, by putting the labels in rows of the tables. If the labels are empty, you can delete the table rows and iOS will dynamically resize the table height for you.
I've been looking around on-line for a solution to this, but I can't seem to find anything, and I'm stuck.
I have a NSMutableArray:
productsToDisplay
Which consists of a heap of Product objects that each have a 'name' attribute e.g.
Product *product = ....
NSLog(#"%#", product.name);
Each cell displays a products name and when clicked, displays more information about the product.
However I have a lot of products and would like to split them up into alphabetical sections in the UITableView (like the Contacts on your iPhone are).
Can someone point me in the right direction?
Thanks a lot,
Jack
[EDIT]
I realise I could just sort the array on the name attribute, but I would also like the quick search bar that runs down the right hand side of the UITableView so a user can easily find the section they are looking for.
use this delegate to display the right hand sections list:-
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return searchArray;
}
use this delegate to associate table content (section) according to right hand list:-
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
if (index == 0) {
[tableView scrollRectToVisible:[[tableView tableHeaderView] bounds] animated:NO];
return -1;
}
return index;
}
Add alphabetic sections to ur tableview, then You have to create array of alphabets corresponding to the section in tableview. Like section[0] - 'A', section[1] - 'B', etc.
Add
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return keys;
}
//the delegate is asking for an array of the values to display in the index.
So the first item in this array will take the user to the first section, which is section 0.
I have a UITableView that, under certain conditions, needs to have something added to the top of it. All data (except for what is inserted at the top of the UITableView under certain conditions) is brought in from an array.
Because everything is brought in from an array, I need to modify the indexPath that fetches those array objects each and every time the method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is called. If I try to create a local variable and update the local version of indexPath.row, it tells me it is read only.
What would be the best way to implement this?
Drawing below (this is not intended to be code, but a drawing of the table view):
(REGULAR SITUATION) (3 lines)
array objectAtIndex:0;
-----
array objectAtIndex:1;
-----
array objectAtIndex:2;
etc. etc
(MODIFIED SITUATION) (4 lines)
blah blah modified insertion text here
-----
array objectAtIndex:0;
-----
array objectAtIndex:1;
-----
array objectAtIndex:2;
etc etc
Thanks in advance.
Why not just add your new item at the start of your array?
// Create a mutable copy and add the item at index 0
NSMutableArray *mutable = [myData mutableCopy];
[mutable insertObject:newItem atIndex:0];
// Then store the new array and reload the table
[myData autorelease];
myData = mutable;
[self.tableView reloadData];
Then you don't have to do anything funny at all with index paths :)
THIS is a great tutorial that addresses your issue.
Use
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
OR
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Return the String/View as something or empty depending on the condition that you use while deciding when to show it or not.
Don't go down that path. Just use the tableHeaderView property of UITableView to add something on top of the table. It will scroll just like a UITableViewCell.
self.tableView.tableHeaderView = aView;
To remove it, just set it to nil.
If you insist in this method, just keep a BOOL around to tell you in which state you are and if you need the extra line just subtract 1 from indexPath.row, like
[myArray objectAtIndex:indexPath.row-1];
I know its possible to create a table that has an index on the side and a search bar at the top that a user can type in to find an item, but is it possible to say to the table if array isEqual to "item1" push view1? I would like to push a different view with each cell. Anyone have any advice?
Sure. Just create the appropriate view (controller) depending on the cell's indexPath in tableView:didSelectRowAtIndexPath:.
Create the cell based on the index path. If you create all the cells ahead of time, store them in an array by row index. If you are creating them as needed, do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *result;
switch ( [indexPath row] ) {
default: result = [self tableView:tableView normalCellForRowAtIndexPath:indexPath]; break;
case 3: result = [self tableView:tableView detail3CellForRowAtIndexPath:indexPath]; break;
case 5: result = [self tableView:tableView detail5CellForRowAtIndexPath:indexPath]; break;
}
return result;
}
Check out the sample programs from the book Beginning iPhone Development. You must sign up, but its free. You should look specifically at chapter 9's sample code called Nav. Shows you exactly what you need. This was my first book on the subject and was well worth it.
http://www.iphonedevbook.com/forum/viewforum.php?sid=3010c045df967353c6b3894889e6b8f5
Cheers!
Ken
In my app, I have a table displaying a cell in each row.
In Interface Builder, I dragged a button onto the cell, styled it as a Dark Info button, and connected it to a IBAction.
That is working fine.
Only, I want the button to behave differently, depending on the row of the table where the cell of the button is.
How would I get that row index?
I realize that I might display a lack of basic understanding of the object hierarchy, but I hope you guys will forgive me
Thanks
Sjakelien
It's definitely not easy to do if you don't have some data set up first. If you can, have an NSDictionary where the buttons are the keys and the values are the index paths, that you update whenever you return a cell from -tableView:cellForRowAtIndexPath:. Something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
[indexDict setValue:indexPath forKey:theButton];
return cell;
}
- (void) buttonPressed:(UIButton *)button {
NSIndexPath *indexPath = [indexDict valueForKey:button];
...
}
You can maintain tags. When you drag and drop the button, check the interface build and you will see "tag" property for these buttons. Assign different values for each of your button ( I assume you have different buttons for different rows, this solution will not work if you have same cell identifier for different rows ). And when you receive an event check for tag value.
I had similar problem with my work and i was maintaing NSArray for each button tag created.
In your tableView delegate and datasource methods (check the docs!) you have several methods, the best one for this is
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Drop this method in your implementation and say something like
switch (indexPath.row) {
case 0:
//set variable or do method based on row 0 (first row)
break;
case 1:
//set variable or do method based on row 1 (second row)
break;
case 2:
//set variable or do method based on row 2 (third row)
break;
}//and so on
}
another way is to change the base class of your UIButton in your view,
then using this other class wich basically extends an UIButton with an added NSInteger row #property (remember to #synthesize it).
You'll then set this property during the cell setup, and you can retrieve this property within the message method