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.b开发者_JS百科ooks2 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 sayallData
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;
}
精彩评论