在IOS中使用动态数据创建表的建议

时间:2012-04-19 13:37:44

标签: ios objective-c xcode cocoa-touch ios5

我正在尝试创建一个包含动态数据的表格,我有点卡住了。以下是我的数据结构:

NSMutableArray *bigArray;

bigArray有很多NSDictionary项。

每个items只有一个条目。

sectionName是关键,NSMutableArray是值。

value NSMutableArray中有许多对象。

我试着尽可能简单地解释这一点,这是我被卡住的部分。

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

//medium
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{    
    // Return the number of rows in the section.
    return [[[[bigArray objectAtIndex:section] allValues] objectAtIndex:0] count];
}

我无法弄清楚如何根据我当前的数据结构实现此方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [tableView 
                             dequeueReusableCellWithIdentifier:@"MyCell"];

    MyObject *obj = //Need this part


    cell.textLabel.text = obj.name;   

    return cell;

}

简单地说,我试图插入动态数据的动态部分。我正在寻找更有经验的开发人员的建议,你会如何解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

假设我很清楚您的数据是如何构建的,请按以下步骤操作:

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
    }

    //this will get you the dictionary for the section being filled
    NSDictionary *item = [bigArray objectAtIndex:indexPath.section];
    // then the array of object for the section
    NSMutableArray *mutableArray = [item objectForKey:@"sectionName"];
    //you then take the object for the row
    MyObject *obj = [mutableArray objectAtIndex:indexPath.row];

    cell.textLabel.text = obj.name;   

    return cell;
}

不要忘记在属性检查器

中设置单元原型的重用标识符