About sectioning your UITableView - objective-c

I'm working on a tableview, which displays more than one line of text in its cell. I want to have two sections, which I've already done. But is it possible to display another array in the second section, not the same? T
Here's the code I use:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return MIN([titles count], [subtitles count]);
}
else {
return 1;
}
}

Of course you can do this.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return MIN([titles count], [subtitles count]);
}
else {
return MIN([titles2 count], [subtitles2 count]);
}
}
Just make sure to switch arrays in all UITableViewDataSource and UITableViewDelegate methods.
But I would suggest nested arrays. It makes the code shorter, because when you have more than 10 sections your code will become long and ugly.
And with nested arrays you only have the long ugly code in viewDidLoad or whereever you initialize your array;
titles = [NSArray arrayWithObjects:titles0, titles1, nil];
subtitles = [NSArray arrayWithObjects:subtitles0, subtitles1, nil];
.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return MIN([[titles objectAtIndex:section] count], [[subtitles objectAtIndex:section]count]);
}

Related

UISearchController – Search result show different records

When i type "stefanik" in searchbar the result is one record (but different look), when i tap on this, is the right record, see screenshots.
I think problem is in these methods, but i cant find the problem.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.searchController.active)
{
return 1;
}
else
{
return [[self.fetchedResultsController sections] count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.searchController.active)
{
return [self.filteredList count];
}
else
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
}
Have you seen this problem before?
Screenshots
If you are using NSFetchedResultsController apply an NSPredicate to filter the records for example
if (self.searchController.active) {
self.fetchedResultsController.fetchRequest.predicate = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind){
return [[(Airport *)obj name] rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound;
}];
} else {
self.fetchedResultsController.fetchRequest.predicate = nil
}
// reload data
This is more efficient than filtering in code into an extra array

TableView not reloading properly

I am making an settings screen in which you can select stations via a uisearchbar. I have a sectioned tableview, with the first letter of a station as the header and every station is categorized by it's first letter. So far so good.
I habe 2 NSMutableArray's with, per section, the stations. One is the unfiltered array (Which I use when I don't have it filtered) and the other one, when I am searching for something. (I do this via a predicate). On every keypress on the keyboard I do a [self.tableView reloadData]; this works, HOWEVER the scrollview stays too long! So you can scroll way past how many results are actually in the selected array. This causes a crash, because it's trying to get objects that don't exist.
So it seems like the tableview isn't counting the array right or something?
Is anyone familiar with this problem?
Here is some code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.searching) {
return [self.tableFilterd count];
} else {
return [self.tableData count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Rows for section");
// Return the number of rows in the section.
if (self.searching) {
NSLog(#"Editing section: %i, count %i", section, [[self.tableFilterd objectAtIndex:section] count]);
return [[self.tableFilterd objectAtIndex:section] count];
} else {
NSLog(#"Not editing");
return [[self.tableData objectAtIndex:section] count];
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
SettingsHeaderCell *cell = [[[SettingsHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"HeaderCell"] autorelease];
cell.labelLetter.text = [[self.tableLetters objectAtIndex:section] capitalizedString];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 52;
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
SettingsCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[SettingsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (self.searching) {
StationObject *object = (StationObject *)[[self.tableFilterd objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[cell setStationObject:object];
} else {
StationObject *object = (StationObject *)[[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[cell setStationObject:object];
}
return cell;
}
You might have solved this by now but I suspect you aren't emptying either arrays. In the method:
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
//Remove all objects first
[self.tableFiltered removeAllObjects];
[self.tableData removeAllObjects];
Also you only need to call [self.tableView reloadData]; in textDidChange, not in the other three methods. Hope this helps.

How to create sections for parsed Data in objective c

I have parsed data which I put in tableview alphabetically. Alphabetical order is based on the last name of a person. Now I need to add index bar and sections. What I am creating is kind of address book. This is the code I am using to put the parsed data in the cell label.text = [arrayOfTitle objectAtIndex:indexPath.row];
This is the code through which I placed index bar and section titles on the tableview:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self arrayOfNames] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 26;
}
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
if ([self.arrayOfNames objectAtIndex:section] ) {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles]
objectAtIndex:section];
}
return nil;
}
#end
I know how to create sections out of plist file but since here I just have an array of parsed data, I am having a hard time to break it into sections.
Thank you for your help.

Remove sections with no rows from UITableView

I would like to remove section headers from a UITableView if there are no rows for that section.
I'm using UILocalizedIndexedCollation for my section headers. So when I create the headers, I don't necessarily know what sections will have content.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
//return [customerSections count];
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 1;
}
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//NSLog(#"Section: %i", section);
if (tableView == self.searchDisplayController.searchResultsTableView) {
return self.filteredCustomers.count;
} else {
return [[self.customerData objectAtIndex:section] count];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// The header for the section is the region name -- get this from the region at the section index.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return nil;//#"Results";
}
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//return [customerSections allKeys];
if (tableView == self.searchDisplayController.searchResultsTableView) {
return nil;
}
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
Just wanted to chime in and give my solution to this
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([self.myTableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
return nil;
}
return [[self.collation sectionTitles] objectAtIndex:section];
}
based on this answer
I've recently achieved this with this code:
This is the function that returns the title for the section, if there are no rows in that section then don't return a title:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if ([self.customerData count] > 0 ) {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
return nil;
}
It's an interesting question, with a number of possible solutions.
Working backwards, numberOfSectionsinTableView and numberOfRowsInSection are what need to be updated in order to display the right number of sections. These are partly dependent on the methods UILocalizedIndexedCollation methods.
(Presumably this happens after some sort of user action (a delete or an insert), so note that in commitEditingStyle you should call [self.tableView reloadData).
I am assuming that customerData is an array, where at each index there is a mutable array that corresponds to a section. When the array at a certain customerData index has no data, you want to remove the section for that index.
The solution then, is to calculate everything manually - determine section information based on lives inside your customerData array.
I took a stab at rewriting three of your methods. Good luck!
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return nil;
}
NSMutableArray *arrayToFilter = [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
//This is the key - recalculate index titles based on what's present in customerData
for (int i = [self.customerData count] -1; i >=0 ; i--) {
if (![[self.customerData objectAtIndex:i] count]) {
[self.arrayToFilter removeObjectAtIndex:i];
}
}
return arrayToFilter;
}
//You need to be calculating your table properties based on the number of objects
//rather of the 'alphabet' being used.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
//return [customerSections count];
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 1;
}
//return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
int populatedArrayCounter = 0;
for (int i = 0; i <[self.customerData count]; i++) {
if ([[self.customerData objectAtIndex:i] count]) {
populatedArrayCounter++;
}
}
return populatedArrayCounter;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//NSLog(#"Section: %i", section);
if (tableView == self.searchDisplayController.searchResultsTableView) {
return self.filteredCustomers.count;
} else {
// Your original line requires changing, because there are customerData array objects with a count of 0, and you don't want any sections like that
// Thus, pick from the set of populated arrays.
NSMutableArray populatedArrays = [[NSMutableArray alloc] init];
for (int i = 0; i <[self.customerData count]; i++) {
if ([[self.customerData objectAtIndex:i] count]) {
[populatedArrays addObject:i];
}
}
return [[populatedArrays objectAtIndex:section] count];;
}
}
I wanted to share my solution in swift
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if self.sections[section].isEmpty
{
return nil
}
else
{
return collation.sectionTitles[section]
}
}
I ended up removing the unused sectionIndexTitles and creating the sections based on that.
In my NSURLConnection requestDidFinish I used the following.
self.customerData = [self partitionObjects:[self customers] collationStringSelector:#selector(self)];
then had
-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
sectionIndexTitles = [NSMutableArray arrayWithArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[collation sectionTitles] count];
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
for (int i = 0; i < sectionCount; i++) {
[unsortedSections addObject:[NSMutableArray array]];
}
for (id object in array) {
NSInteger index = [collation sectionForObject:[object objectForKey:#"name"] collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray array];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSUInteger lastIndex = 0;
NSMutableIndexSet *sectionsToRemove = [NSMutableIndexSet indexSet];
for (NSArray *section in unsortedSections) {
if ([section count] == 0) {
NSRange range = NSMakeRange(lastIndex, [unsortedSections count] - lastIndex);
[sectionsToRemove addIndex:[unsortedSections indexOfObject:section inRange:range]];
lastIndex = [sectionsToRemove lastIndex] + 1;
} else {
NSArray *sortedArray = [section sortedArrayUsingDescriptors:sortDescriptors];
[sections addObject:sortedArray];
}
}
[sectionIndexTitles removeObjectsAtIndexes:sectionsToRemove];
return sections;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (self.searchDisplayController.active) {
return 1;
}
return [sectionIndexTitles count];//[[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.searchDisplayController.active) {
return self.filteredCustomers.count;
} else {
return [[self.customerData objectAtIndex:section] count];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (self.searchDisplayController.active) {
return nil;
}
return [sectionIndexTitles objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (self.searchDisplayController.active) {
return nil;
}
return sectionIndexTitles;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;//[[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
And with all the above code this removed the unused letters from the right side of the screen as well as the section headers that had no rows.

shouldReloadTableForSearchString doesn't show filtered result

I have been trying for hours but couldn't figure out why it doesn't show the filtered result. It always shows all the results. I'm not sure what I am doing wrong.
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
/*
Update the filtered array based on the search text and scope.
*/
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
//for loop here
NSLog(#"%i", [filteredListContent count]);
//filteredListContent contains correct number of filtered items
}
It works. I wasn't using the filtered result array in numberOfRowsInSection method
was:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.persons count];
}
changed to:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.filteredListContent count];
}
else
{
return [self.persons count];
}
}