【发布时间】:2012-12-08 09:36:55
【问题描述】:
我有一个带有字典和每个字典的字符串数的 plist。显示到下面的 url 中。这个项目列表在 plist 中有数千个。
现在想将这些列表显示到表格视图中
.
现在我如何将这个plist 显示到UITableView 中
我正在尝试的是:
- (id)readPlist:(NSString *)fileName
{
NSString *error;
NSPropertyListFormat format;
id plist;
NSString *localizedPath = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"plist"];
dic =[NSDictionary dictionaryWithContentsOfFile:localizedPath];
plist = [NSPropertyListSerialization propertyListFromData:dic mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
if (!plist) {
NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
[error release];
}
return plist;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
dict =[self readPlist:@"A"];
return dict.allKeys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
dict = [self readPlist:@"A"];
key = [dict.allKeys objectAtIndex:section];
return [[dict valueForKey:key] count];
}
- (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] autorelease];
}
cell.textLabel.text = [[dict objectForKey:key] objectAtIndex:indexPath.row];
return cell;
}
【问题讨论】:
-
不要调用 readPlist: 方法的所有项目。取而代之的是,在接口文件中声明一个字典并使用它。因为它会像你说的那样产生性能问题,所以你的 plist 中有成千上万的数据。
-
你是如何为你的 tableView 设置 Delegate 和 Datasource 的?
-
是的...我已经这样做了
-
我问你是怎么做到的?该委托方法是否被调用??
-
sorry 8000 item with 53 record in each item -> 8000*53 = 42400 records in your plist file..令人震惊..这真的很尴尬..:-{
标签: iphone ios ipad uitableview plist