0

次のようにテーブルビューにデータをロードします。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [tableData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[[tableData objectAtIndex: section] objectForKey: @"Rows"] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[tableData objectAtIndex: section] objectForKey: @"Title"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];

    return cell;
}

私のビューDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableData = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"data" ofType: @"plist"]];
}

ARCモードを使用しています

編集

私のリスト:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>Title</key>
        <string>Section1</string>
        <key>Rows</key>
        <array>
            <string>Section1 Item1</string>
            <string>Section1 Item2</string>
        </array>
    </dict>
    <dict>
        <key>Title</key>
        <string>Section2</string>
        <key>Rows</key>
        <array>
            <string>Section2 Item1</string>
            <string>Section2 Item2</string>
        </array>
    </dict>
</array>
</plist>
4

2 に答える 2

0

このtableDataはNSDictionaryです:

 - (void)viewDidLoad {
  [super viewDidLoad];

  self.tableData = [NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"data" ofType: @"plist"]];

}

tableData を使用して tableView に値をフィードし、それに応じて tableViewDelegate と datasource を変更します

于 2012-06-13T16:15:33.533 に答える
0

最初にこれを行いますdidLoad

NSString *dataStr = [NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle]
                                               pathForResource: @"data" ofType: @"plist"]];

それを NSDictionary に追加します。

NSDictionary *dataDict = [[NSDictionary alloc] initWithContentsOfFile:dataStr];

辞書を NSArray に追加します。

NSArray *dataArray = [dataDict allKeys]

最後に:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

次のようにして、テーブルにデータを入力します。

 NSString *data = [dataArray objectAtIndex:[indexPath row]];
 [[cell textLabel] setText:data];
于 2012-06-13T16:29:27.367 に答える