繁体   English   中英

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

[英]Advice on creating table with dynamic data in IOS

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

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;

}

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

假设我很清楚你的数据是如何构建的,这就是我将如何做到的:

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

不要忘记在属性检查器中设置单元原型的重用标识符

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM