簡體   English   中英

靜態表格檢視

[英]Static Table View

我正在創建一個靜態表視圖(必須與iOS 4兼容-所以我不能使用iOS 5的方法)。

我有兩種方式: 第一個有一個單元格,第二個有兩個單元格。 我制作了兩個數組,一個數組在第一節中具有唯一單元格的標題,第二個數組在第二節中具有兩個單元格的兩個標題。 所以我的字典看起來像這樣:

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

我遇到的問題是,我需要使用tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section返回節中的行數。 所以我的問題是,如何使用NSInteger section從字典中檢索該NSInteger section 我還必須在tableView:cellForRowAtIndexPath做同樣的事情。

謝謝

如果您不了解字典的工作方式,建議您簡化一下問題。 為每個部分創建一個數組,然后在委托方法內部使用switch()語句為行計數等調用[array count]。對於部分計數,您仍然可以使用具有[[dictionary allKeys] count]的原始字典。

編輯:我只是看到@fzwo在兩個評論中推薦相同的東西

如前所述,最好的選擇是數組數組。 為了避免使用字典的復雜性,請為表數據和節標題創建兩個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];

如果需要更多可用於單元格的數據,則可以使用其他對象代替行標題。

要獲取您部分中的行數,可以使用以下命令:

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

並獲取單元格值:

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