显示来自不同NSMutableArrays的表视图部分中的数据

时间:2011-08-09 08:05:40

标签: iphone objective-c

我从JSON获取数据并将该数据存储在10个不同的NSMutable Arrays中。我想在tableview的10个部分中显示该数据,以便如何执行此操作

- (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];
}

}

如果我在下面做,它只显示第一部分

       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;

5 个答案:

答案 0 :(得分:2)

在我看来(如果你不想在你的代码中做太多改变),就像这样做

  • 制作另一个NSMutableArray对象,让我们说allData
  • 根据部分将所有数组添加到allData中,如此

    [allDatad addObjects:appDelegate.books1,appDelegate.books2,appDelegate.books3...,appDelegate.books10,nil];
    

现在在cellForRowAtIndexPath只需更改此

Book1* aBook=[[allData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
int count=[[allData objectAtIndex:indexPath.section] count];

并在

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[allData objectAtIndex:indexPath.section] count];
}

答案 1 :(得分:0)

使用indexPath.section确定准备单元格的部分。

例如,如果indexPath.section为3,则使用appDelegate.books4。

希望这有帮助。

答案 2 :(得分:0)

更好的可扩展实现是将这些数组添加到另一个数组,比如arrayOfArrays。现在您的部分数量是

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return [arrayOfArrays count];
}

行数为:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[arrayOfArrays objectAtIndex:section] count];
}

并且你的cellForRowAtIndexPath是:

- (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;
}

答案 3 :(得分:0)

    - (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;    

}

答案 4 :(得分:0)

您应该将所有这些数组存储在NSMutableDictionary中,例如

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:appDelegate.books1 forKey:@"1"];
    [dict setObject:appDelegate.books2 forKey:@"2"];
    [dict setObject:appDelegate.books3 forKey:@"3"];

并在创建像

这样的单元格时使用indexPath.section的引用进行访问
    Book1*aBook = [dict valueForKey:[NSString stringWithFormat:@"%i", indexPath.section + 1]];

当您想要定义numberOfRowsInSection时,您可以从NSMutableDictionary获取计数。

    Book1*aBook = [dict valueForKey:[NSString stringWithFormat:@"%i", section + 1]];
    [aBook count];
相关问题