1

我有一个表格视图,我在其中动态返回每个部分的节数和行数。我可以这样实现

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(  section == 0)
    return [array count];

    if(  section == 1)
    return [array2 count];

    if(  section == 2)
    return [array3 count];
}

如果我知道我有 3 个部分,我可以按照上述方式进行操作。但是如何动态定义不同部分的行数。例如:- 你有 if(section == 0)。我不想在这里硬编码 0,1,2 的值。我不确定部分的总数。我们要如何动态返回不同部分的行数?我有一个数组来显示没有的部分和不同的数组来显示每个部分的行数。如何在方法“numberOfRowsInSection”中动态提及?

4

1 回答 1

7

在这里您可以遵循这种方法,您可以将所有可数数组放在另一个中NSMutableArray,例如 tmpArray (例如,tmpArray[0] = arraytmpArray[1] = array2,或仅通过附加[tmpArray addObject:array] ..)

然后你可以这样

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

我希望这能帮助你解决你的问题..

于 2012-05-02T14:42:56.843 回答