我正在尝试在自定义UITableViewCell中显示plist文件中的数据。
这是我的rootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"projects" ofType:@"plist"];
projects = [NSArray arrayWithContentsOfFile:path];
}
- (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];
int n = indexPath.row;
for (n=1; n<4; n = n+1) {
NSDictionary *temp = [projects objectAtIndex:indexPath.row];
projectCell.name.text = [temp valueForKey:@"name"];
}
}
return projectCell;
}
我创建的plist文件只包含4个词典。每一个都包含2个字符串。但我只希望其中一个人进入牢房。 (我已经创建了名为“ProjectCell”的UITableViewCell文件。
我刚开始,所以我的问题可能非常基本。但是我花了很多时间试图解决这个问题。
有人可以帮帮我吗?
编辑:当我构建并运行时,它总是给我一张空白表
EDIT2:ProjectCell的接口
@interface ProjectCell : UITableViewCell {
IBOutlet UILabel *name;
IBOutlet UITableViewCell *theCell;
}
@property (nonatomic,retain) IBOutlet UILabel *name;
@property (nonatomic,retain) IBOutlet UITableViewCell *theCell;
@end
EDIT3 ProjectCell.m
@implementation ProjectCell
@synthesize name,theCell;
@end
答案 0 :(得分:1)
你最好稍微调整一下XIB。
UITableViewCell
对象,使用身份检查器将其类更改为“ProjectCell”。 IBOutlet name
从此对象连接到现有标签。File's Owner
并将其重新设置为NSObject。现在是您的ProjectCell.h
,
@interface ProjectCell : UITableViewCell {
IBOutlet UILabel *name;
}
@property (nonatomic,retain) IBOutlet UILabel *name;
@end
和您的ProjectCell.m
,
@implementation ProjectCell
@synthesize name;
@end
最终的更改将采用tableView:cellForRowAtIndexPath:
方法,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ProjectCell";
ProjectCell *cell = (ProjectCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@”ProjectCell”
owner:nil
options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[ProjectCell class]])
{
cell = (ProjectCell *)currentObject;
break;
}
}
}
NSDictionary *temp = [projects objectAtIndex:indexPath.row];
cell.name.text = [temp valueForKey:@"name"];
return cell;
}
答案 1 :(得分:0)
您需要使用自定义的ProjectCell类,而不是实例化UITableViewCell对象。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ProjectCell";
ProjectCell *cell = (ProjectCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[ProjectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *temp = [projects objectAtIndex:indexPath.row];
cell.name.text = [temp valueForKey:@"name"];
return cell;
}
答案 2 :(得分:0)
.M
- (void)viewDidLoad
{
NSString *mylist = [[NSBundle mainBundle] pathForResource:@"Name of your plist file" ofType:@"plist"];
tabledata = [[NSArray alloc]initWithContentsOfFile:mylist];
NSLog(@"%@", tabledata);
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [tabledata count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if( cell == nil )
{
NSLog(@"Cell Creation");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
//Set Data For each Cell
cell.textLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellName"];
cell.detailTextLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellSubtitle"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
@end
·H
#import <UIKit/UIKit.h>
@interface plistTableViewController : UITableViewController
{
NSArray *tabledata;
}
@end