Search Results not visible - iphone

I have a table view with a search display controller. When I type a search the log shows me the search is working and filtering properly however the screen only shows "No Results". I'm using Core Data if that makes a difference.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
}
else{
return[[self.fetchedResultsController sections]count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
return [secInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = #"Cell";
if (tableView != self.tableView) {
NSLog(#"Found searchDisplayController");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSLog(#"Found regular table View");
}
// Configure the cell...
Instance *instance = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = instance.name;
}
Here are NumberOfRowsInSection and Number:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
}
else{
return[[self.fetchedResultsController sections]count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
return [secInfo numberOfObjects];
}

Check your implementation of numberOfSectionsInTableView and numberOfRowsInSection.
In the first, you should return 1 if it's the search table view (1 section).
In numberOfRowsInSection you should also check the given tableView argument as you did in numberOfSectionsInTableView and return [searchResults count].

Shouldn't your implementation be something like :-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 1;
}
else
{
return[[self.fetchedResultsController sections]count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.searchDisplayController.searchResultsTableView)
{
return [searchResults count];
}
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
return [secInfo numberOfObjects];
}
If the number of search results is high, then I would suggest using another Fetched Results Controller instead of an array.

Related

UISearchBar with scope buttons NSInternalInconsistencyException Crash on Cancel

I'm at sort of a loss here.
I have a UISearchBar with two scope buttons Option A and Option B. Searching works just fine on both scope buttons but when I click Cancel out of the search bar with the second scope button selected the app will sometimes crash with this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 3 in section at index 0'
Here's the relevant code for my UITableView:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSArray *results = [fetchedResultsController fetchedObjects];
return [[fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (tableView == self.tableView && [[fetchedResultsController sections] count] > section) {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}
return nil;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSArray *alphabet = [NSArray arrayWithObjects:UITableViewIndexSearch, #"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];
if (tableView == self.tableView) {
return alphabet;
}
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
if(tableView == self.tableView && [title isEqual:UITableViewIndexSearch]){
[tableView scrollRectToVisible:[[tableView tableHeaderView] bounds] animated:NO];
return -1;
} else if (tableView == self.tableView) {
return [self.fetchedResultsController.sectionIndexTitles indexOfObject:title];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"OptionACell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSManagedObject *category = [fetchedResultsController objectAtIndexPath:indexPath];
if (self.searchBar.selectedScopeButtonIndex == CategoriesViewSearchScopeOptionB && ![self.searchBar.text isEqual:#""]) {
[cell.textLabel setText:[category valueForKey:#"text"]];
} else {
[cell.textLabel setText:[category valueForKey:#"name"]];
}
return cell;
}
And the UISearchBar:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if([self.searchBar.text isEqual: #""] && self.searchBar.selectedScopeButtonIndex == CategoriesViewSearchScopeOptionB) {
self.searchBar.text=#"";
[self updateOptionAFetchRequest];
} else {
[self updateFetchRequest];
NSFetchedResultsController *fetchController = [self fetchedResultsController];
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
[self logSearchTerm:self.searchBar.text];
self.searchBar.text = #"";
self.searchBar.selectedScopeButtonIndex = CategoriesViewSearchScopeOptionA;
[self updateOptionAFetchRequest];
self.searchDisplayController.delegate = nil;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
[self logSearchTerm:self.searchBar.text];
if([self.searchBar.text isEqual: #""]) self.searchBar.selectedScopeButtonIndex = CategoriesViewSearchScopeOptionA;
return YES;
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
if([self.searchBar.text length] > 0) {
[self updateFetchRequest];
}
}
- (void)updateFetchRequest {
if (self.searchBar.selectedScopeButtonIndex == CategoriesViewSearchScopeOptionB) {
[self updateOptionBFetchRequest];
return;
}
[self updateOptionAFetchRequest];
}
I'm having trouble discerning the exact pattern of the crashing. It seems to crash consistently whenever I perform the action of opening the UISearchBar, clicking OptionB then Canceling. But when I open the SearchBar, click back and forth between the scope buttons and perform a couple searches, I'm able to close no problem.
Any help from Core Data experts would be greatly appreciated.

assigning datasource to two tableviews in one view controller

Whats wrong in the following code?
here i am using two table views and assigning them with different data source, but its not working
-(void)viewDidLoad
{
[super viewDidLoad];
arryData = [[NSArray alloc] initWithObjects:#"MCA",#"MBA",#"BTech",#"MTech",nil];
arryData2 = [[NSArray alloc] initWithObjects:#"Objective C",#"C++",#"C#",#".net",nil];
flag=1;
myTable1.hidden=YES;
myTable2.hidden=YES;
btnOne.layer.cornerRadius=8;
btnTwo.layer.cornerRadius=8;
myTable1.layer.cornerRadius=8;
myTable2.layer.cornerRadius=8;
myTable1.delegate=self;
myTable1.dataSource=self;
myTable2.delegate=self;
myTable2.dataSource=self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.myTable1=tableView)
{
return [arryData count];
}
else {
return [arryData2 count];
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==self.myTable1)
{
static NSString *CellIdentifier1 = #"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
}
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
NSLog(#"1here is%i %#",indexPath.row,cell.textLabel.text);
return cell;
}
else
{
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
}
cell.textLabel.text = [arryData2 objectAtIndex:indexPath.row];
return cell;
}
}
-(IBAction)btnCategory:(id)sender
{
if (flag==1) {
flag=0;
myTable1.hidden=NO;
myTable2.hidden=YES;
}
else{
flag=1;
myTable1.hidden=YES;
myTable2.hidden=YES;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
myTable1.hidden=YES;
myTable2.hidden=YES;
}
-(IBAction)btnTopic:(id)sender
{
if (flag==1) {
flag=0;
myTable2.hidden=NO;
myTable1.hidden=YES;
}
else{
flag=1;
myTable2.hidden=YES;
myTable1.hidden=YES;
}
}
</code>
when i remove datasource of mytable2 all data gets added to table1 else nothing gets loaded in none of the table.
You did mistake. See my code..
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.myTable1==tableView) // ----- Change here you need to write == symbol instead of = symbol
{
return [arryData count];
} else {
return [arryData2 count];
}
}
Try this,
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==myTable1)
{
return [arryData count];
}
else {
return [arryData2 count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier= #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
if(myTable1==tableView)
{
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
NSLog(#"1here is%i %#",indexPath.row,cell.textLabel.text);
}
else
{
cell.textLabel.text = [arryData2 objectAtIndex:indexPath.row];
}
return cell;
}

How to perform Validation in ios uitableview (group table)?

In my app i have used UItableview group table.
In which there are two section and i want to perform validation on it.
for eg:In section 1 there are two row's,if user select row one user have to select some value from section 2 and if user have select second row in 1 section then no need of selection in section two.
following is my code:
- (void)viewDidLoad
{
NSArray *arr_data1 =[[NSArray alloc]initWithObjects:#"Yes",#"No",nil];
NSArray *arr_data2 =[[NSArray alloc] initWithObjects:#"Monotherapy",#"Adjunctive",nil];
NSDictionary *temp =[[NSDictionary alloc]
initWithObjectsAndKeys:arr_data1,#"",arr_data2,
#"Was it monotherapy or adjunctive",nil];
self.tableContents =temp;
self.arr_data =[[self.tableContents allKeys]
sortedArrayUsingSelector:#selector(compare:)];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.arr_data count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.arr_data objectAtIndex:section];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:[indexPath section]]];
NSUInteger row = [indexPath row];
NSLog(#"fdfdfdf=%d",[indexPath row]);
NSString *rowValue = [listData objectAtIndex:row];
if ([rowValue isEqualToString:#"Yes"])
{
NSString *message = [[NSString alloc] initWithFormat:#"%#",rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"You selected"
message:message delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
In - (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath ,
you can check the section you need by using the following code:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
//do as per you requirement
}
else if(indexPath.section == 1)
{
//do as per you requirement
}
}
You can first declare a variable count to keep track of how many items are selected in section 2 and isItemRequired as a flag variable and sythesize them, then use these delegates:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
A sample code to begin with:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
isItemRequired = YES;
if(isItemRequired && count==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Item Required" message:#"You must have to select one or more item in section 2" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil ];
[alert show];
}
}
else
{
isItemRequired = NO;
}
}
else if(indexPath.section == 1)
{
count = 0;
for (int i=0; i < [tableView numberOfRowsInSection:1]; i++)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
if(cell.selected==YES)
{
count++;
}
}
}
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 1)
{
count = 0;
for (int i=0; i < [tableView numberOfRowsInSection:1]; i++)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
if(cell.selected==YES)
{
count++;
}
}
if(isItemRequired && count==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Item Required" message:#"Atleast one item should be selected in section 2" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil ];
[alert show];
}
}
}
In the didselect method of UITableView, you needs to check the section number & then check which row is clicked from that section
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
{
if (indexPath.row == 0)
{
//do this
}
else if (indexPath.row == 1)
{
//do this
}
}
else
{
//do this
}
}
In the following delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
add a check with the help of indexPath returned by the delegate, it will tell you that which section's which row is selected like :
if(indexPath.section == 0 && indexPath.row == 0)
{
// Select something from the another section also
}
else if(indexPath.section == 0 && indexPath.row == 1)
{
// No need to select from another section.
}
First Take one BOOL variable with isYes in .h file like bellow...
BOOL isYes;
after in viewDidLoad: method just assign it to NO like bellow..
isYes = NO;
after that in didSelectRowAtIndexPath delegate method just use like bellow...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section == 0 && indexPath.row == 0)
{
isYes = YES;
}
else if(indexPath.section == 0 && indexPath.row == 1)
{
isYes = NO;
}
if(isYes){
if(indexPath.section == 1 )
{
//here user can select the section 2
}
}
}
i hope this help you....

How to implement Alphabetical section headers in a UITableView having content getting from DataBase

Till now I have implemented UITableView by populate the contents from the database
By retrieving in an array from sqlite data base
storedContactsArray = [Sqlitefile selectAllContactsFromDB];
so no multiple sections , section headers and returns storedContactsArray.count as number of rows.
Now i need to populate the same data in table view but The data set in Alpabetical sections in alphabetical order.
I tried with
alphabetsArray =[[NSMutableArray 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];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [alphabetsArray count];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return alphabetsArray;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [alphabetsArray objectAtIndex:section];
}
But in case of numberOfRowsInSection it fails since there is no contacts in storedContactsArray initially
Error occurs: -[__NSArrayM objectAtIndex:]: index 25 beyond bounds for empty array
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[storedContactsArray objectAtIndex:section] count]
}
any advice of use full links pls
To achieve your requirement you need to first separate out all the data in to section as Alphabetical order. which is as following.
Here section is a Mutable Dictionary in which we will get all data as alphabetical sets.
//Inside ViewDidLoad Method
sections = [[NSMutableDictionary alloc] init]; ///Global Object
BOOL found;
for (NSString *temp in arrayYourData)
{
NSString *c = [temp substringToIndex:1];
found = NO;
for (NSString *str in [sections allKeys])
{
if ([str isEqualToString:c])
{
found = YES;
}
}
if (!found)
{
[sections setValue:[[NSMutableArray alloc] init] forKey:c];
}
}
for (NSString *temp in arrayYourData)
{
[[sections objectForKey:[temp substringToIndex:1]] addObject:temp];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[sections allKeys]count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* CellIdentifier = #"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == Nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *titleText = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = titleText;
return cell;
}
Please try it I am using it and it working fine
Hope it helps you !!!

showing data in table view sections from different NSMutableArrays

I am fetching data form JSON and storing that data in 10 differnet NSMutable Arrays .I want to show that data in 10 sections of the tableview so how to do this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
//return categories.count ;
if(section == 0)
{
return [appDelegate.books1 count];
}
else if(section == 1)
{
return [appDelegate.books2 count];
}
else if(section == 2)
{
return [appDelegate.books3 count];
}
else if(section == 3)
{
return [appDelegate.books4 count];
}
else if(section == 4)
{
return [appDelegate.books5 count];
}
else if(section == 5)
{
return [appDelegate.books6 count];
}
else if(section == 6)
{
return [appDelegate.books7 count];
}
else if(section == 7)
{
return [appDelegate.books8 count];
}
else if(section == 8)
{
return [appDelegate.books9 count];
}
else if(section == 9)
{
return [appDelegate.books10 count];
}
}
if i am doing below it shows only first section
Book1*aBook=[appDelegate.books1 objectAtIndex:indexPath.row];
int count=[appDelegate.books1 count];
cell.text = aBook.municipality;
cell.detailTextLabel.text=aBook.title;
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.backgroundColor=[UIColor clearColor];
self.tableView.backgroundColor=[UIColor clearColor];
return cell;
In my view (if you don't want to change much in your code) do it like this
make an another NSMutableArray object lets say allData
add all your array into allData according to section, somthing like this
[allDatad addObjects:appDelegate.books1,appDelegate.books2,appDelegate.books3...,appDelegate.books10,nil];
now in cellForRowAtIndexPath just change this
Book1* aBook=[[allData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
int count=[[allData objectAtIndex:indexPath.section] count];
and in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[allData objectAtIndex:indexPath.section] count];
}
Use indexPath.section to determine for which section you are preparing your cell.
For e.g., if indexPath.section is 3 then use appDelegate.books4.
Hope this helps.
A better more scalable implementation would be to add these arrays to another array, say arrayOfArrays. Now your number of sections are
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [arrayOfArrays count];
}
number of rows are:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[arrayOfArrays objectAtIndex:section] count];
}
and your cellForRowAtIndexPath is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//all the dequeueing and initialization and stuff
Book1 *aBook=[[arrayOfArrays objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
int count=[[arrayOfArrays objectAtIndex:indexPath.section] count];
cell.text = aBook.municipality;
cell.detailTextLabel.text=aBook.title;
//do other stuff
return cell;
}
You should store all those arrays in NSMutableDictionary like,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:appDelegate.books1 forKey:#"1"];
[dict setObject:appDelegate.books2 forKey:#"2"];
[dict setObject:appDelegate.books3 forKey:#"3"];
and access with the reference of indexPath.section while creating cell like,
Book1*aBook = [dict valueForKey:[NSString stringWithFormat:#"%i", indexPath.section + 1]];
and at time when you want to define numberOfRowsInSection you can get count from the NSMutableDictionary.
Book1*aBook = [dict valueForKey:[NSString stringWithFormat:#"%i", section + 1]];
[aBook count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
Book1*aBook;
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:MyIdentifier] autorelease];
}
if(indexPath.section == 0)
{
aBook=[appDelegate.books1 objectAtIndex:indexPath.row];
}
else if(indexPath.section == 1)
{
aBook=[appDelegate.books2 objectAtIndex:indexPath.row];
}
int count=[appDelegate.books1 count];
cell.text = aBook.municipality;
cell.detailTextLabel.text=aBook.title;
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.backgroundColor=[UIColor clearColor];
self.tableView.backgroundColor=[UIColor clearColor];
return cell;
}