【问题标题】:How should I ideally create a well-structured UITableViewDataSource?理想情况下,我应该如何创建结构良好的 UITableViewDataSource?
【发布时间】:2013-02-23 16:40:50
【问题描述】:

我有一个NSArray 从服务器获取的歌曲。那是我的原始数据源。该数组包含具有NSDictionaryNSArray 作为后端的自定义对象。

我想知道使用NSDictionary 实现格式化数据源是否明智。字典将以节标题作为键,并且某个键的值将具有包含该节的行的NSArray

我将遍历我的原始数据源并将其按字母顺序排列到字典中。

我感觉这不是一个可靠的实现,而且非常昂贵。还有比这更可靠的实现吗?

【问题讨论】:

    标签: ios nsarray nsdictionary conceptual


    【解决方案1】:

    对于小表,而不是NSDictionary,我通常使用NSArray,因为字典不保留顺序(而且您可能不想不断地重新排序)。所以我通常有一个节数组,对于每个节条目,我至少有一个节标题和一个行数组。我的行数组有,我需要呈现给定行的信息(例如行的文本等)。

    单独的 row 和 section 对象,您可以将它们实现为 NSDictionary 对象本身(有时在从 JSON 或 XML 解析数据时,这是最简单的),但我通常定义自己的 RowSection对象,例如:

    @interface Row : NSObject
    
    @property (nonatomic, strong) NSString *title;
    @property (nonatomic, strong) NSString *subtitle;
    
    @end
    

    @interface Section : NSObject
    
    @property (nonatomic, strong) NSString *title;
    @property (nonatomic, strong) NSMutableArray *rows;
    
    @end
    

    然后我的表格视图控制器有一个NSArray 用于这些部分:

    @property (nonatomic, strong) NSMutableArray *sections;
    

    我这样填充它:

    self.sections = [NSMutableArray array];
    
    Section *sectionObject;
    
    sectionObject = [[Section alloc] initWithTitle:@"Marx Brothers" rows:nil];
    [sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Chico"   subtitle:@"Leonard Marx"]];
    [sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Harpo"   subtitle:@"Adolph Marx"]];
    [sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Groucho" subtitle:@"Julius Henry Marx"]];
    [sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Zeppo"   subtitle:@"Herbert Manfred Marx"]];
    [self.sections addObject:sectionObject];
    
    sectionObject = [[Section alloc] initWithTitle:@"Three Stooges" rows:nil];
    [sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Moe"   subtitle:@"Moses Harry Horwitz"]];
    [sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Larry" subtitle:@"Louis Feinberg"]];
    [sectionObject.rows addObject:[[Row alloc] initWithTitle:@"Curly" subtitle:@"Jerome Lester \"Jerry\" Horwitz"]];
    [self.sections addObject:sectionObject];
    

    然后我有典型的UITableViewDataSource 方法:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [self.sections count];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        Section *sectionObject = self.sections[section];
        return [sectionObject.rows count];
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        Section *sectionObject = self.sections[section];
        return sectionObject.title;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
        Section *sectionObject = self.sections[indexPath.section];
        Row *rowObject = sectionObject.rows[indexPath.row];
    
        cell.textLabel.text = rowObject.title;
        cell.detailTextLabel.text = rowObject.subtitle;
    
        return cell;
    }
    

    对于更大的数据库数据驱动表,我可能不会将数据保存在数组中,而是使用 Core Data 或 SQLite,但想法是一样的。确保我有 SectionRow 类,使我的表视图控制器代码不言自明,并与数据实现的细节隔离。

    【讨论】:

    • 哇……解释得很好。我没有太多数据,所以我暂时避免使用 Core Data。我认为这是要走的路。非常感谢。
    【解决方案2】:

    你试过 RestKit 吗?您只需要提供 json 编码对象的来源并创建模型类

    【讨论】:

    • 我没有使用 JSON 来获取歌曲。
    【解决方案3】:

    您可以使用模型类,然后使用来自服务器的数据创建此模型类的对象。然后使用模型类对象数组作为 UITableView 的源。
    您也可以考虑 core-data ,在其中创建数据模型和关系非常容易。但是,它带有许多附加功能,这对您来说可能是额外的负担。

    【讨论】:

      猜你喜欢
      • 2011-01-02
      • 2021-07-26
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 2018-01-27
      相关资源
      最近更新 更多