0

我像这样将数据加载到tableview:

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

我的 viewDidLoad

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

首先在你的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 回答
0

做这个tableData是NSDictionary:

 - (void)viewDidLoad {
  [super viewDidLoad];

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

}

使用 tableData 在 tableView 中提供值并相应地更改 tableViewDelegate 和数据源

于 2012-06-13T16:15:33.533 回答