ios - 关于在IOS中创建动态数据表的建议

标签 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;

}

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

最佳答案

假设我很好地理解了您的数据的结构,那么我会这样做:

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

不要忘记在属性检查器中为单元格原型(prototype)设置重用标识符

关于ios - 关于在IOS中创建动态数据表的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10229296/

相关文章:

ios - 如果连接到的 UIImageView 在 UIScrollView 下,是否可以使 Tap Gesture Recognizer 工作?

ios - 在 UITableViewHeaderFooterView 中更改字体大小的问题

javascript - 创建 HTML 和 JS 小部件并将它们集成到 native 和混合应用程序中

ios - ARKit 在场景中放置一个自定义对象

ios - 使用重用标识符的 UITableView cellForRowAtIndexPath 问题

ios - 无法在属性初始值设定项中使用实例成员 'server'

ios - For 循环遍历 TableView Controller 中的单元格

ios - @3x 图像是强制性的吗?

ios - 旋转时的 UICollectionViewCell alpha 值

objective-c - UIButton 不响应 'touchUpInside' 但会响应 'touchDown' 正常