How does one implement a NON alphabetical index for a UITableView? An example of this can be seen in Apple's Remote App. When looking at songs, there is a dotted index on the right hand side that allows the user to scroll quickly through the entire table. There are also no section headers.
Thanks
I'd guess that it works by returning an array full of the “middle dot” character (Unicode 00B7), bookended by the “bullet” character (2022). In the Remote app, the index has 23 items in it; therefore, to do this, you need to split up your data into 23 sections of roughly equal length. As occulus says, returning nil or #"" in -tableView:titleForHeaderInSection: will cause no section headers to be shown.
Return the aforementioned array of characters in your data source’s -sectionIndexTitlesForTableView: method, then implement -tableView:sectionForSectionIndexTitle:atIndex: to return the section index it gives you, and you should get identical behavior to Remote’s.
If you specify #"" for a section title, the section header won't be shown, which will give you the effect you desire. To use table indexing (the right hand side tiny list), you should still organise your table in sections so it knows where to jump when a index is touched, you just have to hide the section titles as I described.
To add index items, see UITableViewDelegate's method:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
Related
I want to create a truncating label that looks and works just like the Section Index Titles List in a Table View. Can this be achieved with a UILabel and how?
To make your table view index look like that one, implement both sectionIndexTitles (to dictate the letters and bullets) and tableView(_:sectionForSectionIndexTitle:at:) (to set up the correspondence with your actual sections).
I have a tableview with 3 sections / 3 customs section header view...
After I delete a row in one of this section and if the section come empty, how I can delete the section header view.
Thanks
One of the approaches could be to maintain flags as to whether the section has become empty and return zero height for that section's header and footer and then execute reloadSections:withRowAnimation: when the only row in section is deleted.
This is certainly better when you have section specific customizations which would become tricky to handle if we were to remove the section from our model. If there are no customizations as such, you could go about maintaining an array of arrays. Once the row array is emptied, you can discard the section from the sections array. This will reflect on reloadData.
In either case, you will need to affect the model to change the view.
what you can do after you delete the row is call the method -(void)reloadData. If you are already doing this, you need to update your datasource to indicate that the row has been deleted and in - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView you need to check that. Let me know if that helps!
One way I have dealt with this problem is implementing the section title like such:
-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return ([[allData objectForKey:[keys objectAtIndex:section]] count] > 0) ? [keys objectAtIndex:section]:#"";
}
This just checks that the array of objects for that section actually has something if it does it returns the name of the section if not it returns an empty string which hides the header title.
The table view does need to be updated for the change to come into effect, also I was a dictionary full of arrays and an array full of the dictionary keys. but something similar could be easily implemented. This method allows for insertion back into that section without having to re-instantiate an array.
Hope it helps.
I'm using the sectionIndexTitlesForTableView method for my custom UITableView, to display A-Z on the side of my table. However, the standard view of this doesn't match the style of the table. Is there a way to change this, or at least move the index a little?
Here's how it overlaps:
I too tried the same thing..but not succedded.
but as a workaround we can introduce spaces before the text which we want to display as a section header, so that we can start index with some gap.
I have a tableview I'd like to customize based on how many rows it has.
If it has no rows, I'd like the background image to prompt the user to add content.
If it has 1 or more rows, I'd like it to have a different background image, in order to display the content.
I'm using a fetched results controller to populate my tableview, by the way.
Any ideas?
Well this is generally very easy to accomplish as you need only to have UITableView properly delegated to your ViewController with appropriate delegate methods included in your .m file.
Then you can anywhere get row count like this:
[tablePropertyName numberOfRowsInSection:SECTION_NUMBER];
where section number is 0 for first section, 1 for second, etc.
In Swift, you can get the UITableView rows count by
yourTableViewName.numberOfRows(inSection: Int)
example:
yourTableViewName.numberOfRows(inSection: 0) // returns rows count in section 0
I agree with Sixten Otto's answer.
[myFetchedResultsController.fetchedObjects count];
However there's more. Above line would return the number of objects regardless the sections. However if you would want to perform this on a table with multiple sections, you would have to call it the following way.
[[myFetchedResultsController.sections objectAtIndex:<section>] numberOfObjects];
You can get the objects for the section like this.
[[myFetchedResultsController.sections objectAtIndex:<section>] objects];
** You have to replace with the number representing the section.
Hope this helps.
I'd recommend taking a look at the documentation for NSFetchedResultsController. It has example code for implementing the methods of UITableViewDataSource (including the ones that say how many sections the table has, and how many rows in each section), as well as documenting the property fetchedObjects, which you could use to see how many raw results you fetched.
[myFetchedResultsController.fetchedObjects count];
tableView.numberOfRows(inSection:) returns numbers of row in section, this method may not trigger fetched results controller's core data query.
let numberOfItems = (0..<tableView.numberOfSections).reduce(into: 0) { partialResult, sectionIndex in
partialResult += tableView.numberOfRows(inSection: sectionIndex)
}
I have a UITableView with some empty sections. I'd like the user to be able to move a row into them using the standard edit mode controls. The only way I can do it so far is to have a dummy row in my "empty" sections and try to hide it by using tableView:heightForRowAtIndexPath: to give the dummy row a height of zero. This seems to leave it as a 1-pixel row. I can probably hide this by making a special type of cell that's just filled with [UIColor groupTableViewBackgroundColor], but is there a better way?
This is all in the grouped mode of UITableView.
UPDATE: Looks like moving rows into empty sections is possible without any tricks, but the "sensitivity" is bad enough that you DO need tricks in order to make it usable for general users (who won't be patient enough to slowly hover the row around the empty section until things click).
I found that in iOS 4.3, the dummy row needs to have a height of at least 1 pixel in order to give the desired effect of allowing a row to be moved into that section.
I also found that the dummy row is only needed in the first and last section; any sections in between don't have this problem.
And it looks like in iOS 5.0, no dummy rows or special tricks are needed at all.
While managing the edit, you can monitor if the table view is in Edit Mode. Use that flag inside of cellForRowAtIndexPath to decide weather or not to display the 'blank' row. While in 'regular' mode, the row will not display, but when the user taps 'edit' cellForRowAtIndexPath should get called again and this time decide to display the row. The details of how to do that depend on your data source and how you are gluing it to the display. If you aren't getting the call again, you can manually inject rows with insertRowsAtIndexPaths / deleteRowsAtIndexPaths and/or call reloadData to force a refresh.
I found that if you return -1.0 from the heightForRowAtIndexPath method it will remove the 1 pixel line.