简体   繁体   English

静态表格检视

[英]Static Table View

I am creating a static table view (must be compatible with iOS 4 - so I can't use iOS 5's method). 我正在创建一个静态表视图(必须与iOS 4兼容-所以我不能使用iOS 5的方法)。

The way I have it is that I have two sections; 我有两种方式: the first has one cell, and the second has two cells. 第一个有一个单元格,第二个有两个单元格。 I made two arrays, one with the title of the only cell in the first section, and the second with both titles for both cells in the second section. 我制作了两个数组,一个数组在第一节中具有唯一单元格的标题,第二个数组在第二节中具有两个单元格的两个标题。 So my dictionary looks like this: 所以我的字典看起来像这样:

(NSDictionary *)  {
    First =     (
        Title1       < --- Array (1 item)
    );
    Second =     (
        "Title1",    < --- Array (2 items)
        Title2   
    );
}

The issue I have is that I need to return number of rows in a section using tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section . 我遇到的问题是,我需要使用tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section返回节中的行数。 So my question is, how do I retrieve the section from the dictionary using NSInteger section ? 所以我的问题是,如何使用NSInteger section从字典中检索该NSInteger section I would also have to do the same thing in tableView:cellForRowAtIndexPath . 我还必须在tableView:cellForRowAtIndexPath做同样的事情。

Thank you 谢谢

If you don't understand how dictionaries work, I'd recommend simplifying the problem. 如果您不了解字典的工作方式,建议您简化一下问题。 Create one array for each section, then inside your delegate methods use a switch() statement to call [array count] for the row count etc. For the section count you could still use the original dictionary with [[dictionary allKeys] count]. 为每个部分创建一个数组,然后在委托方法内部使用switch()语句为行计数等调用[array count]。对于部分计数,您仍然可以使用具有[[dictionary allKeys] count]的原始字典。

EDIT: I just saw @fzwo recommends the same thing in two comments 编辑:我只是看到@fzwo在两个评论中推荐相同的东西

Your best bet is an array of arrays, as has been mentioned. 如前所述,最好的选择是数组数组。 To avoid the complexity with dictionaries, create two NSArray ivars for the table data and the section titles. 为了避免使用字典的复杂性,请为表数据和节标题创建两个NSArray ivars。

// in viewDidLoad

tableData = [NSArray arrayWithObjects:
   [NSArray arrayWithObjects:
      @"Row one title", 
      @"Row two title", 
      nil],
   [NSArray arrayWithObjects:
      @"Row one title", 
      @"Row two title", 
      @"Row three title", 
      nil],
   nil]; 
sectionTitles = [NSArray arrayWithObjects:
   @"Section one title",
   @"Section two title", 
   nil]; 

// in numberOfSections: 
return tableData.count;

// in numberOfRowsInSection:
return [[tableData objectAtIndex:section] count];

// in titleForHeaderInSection:
return [sectionTitles objectAtIndex:section];

// in cellForRowAtIndexPath:
...
cell.textLabel.text = [[tableData objectAtIndex:indexPath.section]
                       objectAtIndex:indexPath.row];

You could use other objects instead of the row titles if you need more data available to your cell. 如果需要更多可用于单元格的数据,则可以使用其他对象代替行标题。

To get the number of rows in your section you can use the following: 要获取您部分中的行数,可以使用以下命令:

tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *key = [[dictionary allKeys] objectAtIndex: section];
    return [[dictionary objectForKey:key] count];
}

And to get the cell value: 并获取单元格值:

tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *key = [[dictionary allKeys] objectAtIndex: indexPath.section];
    NSArray *values = [dictionary objectForKey:key];
    NSString *value = [values objectAtIndex: indexPath.row];

    // code to create a cell

    return cell;
}

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

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