我为plist
创建了tableview
,但我不知道如何将plist
填入Xcode(使用目标c),也不知道我的plist
日期
<plist version="1.0">
<dict>
<key>Robert Mitchall</key>
<array>
<string>8.png</string>
<string>Hey dude!</string>
</array>
<key>Carrie Ronald</key>
<array>
<string>7.png</string>
<string>Lets have a coffe!</string>
</array>
<key>Alicia Donnoven</key>
<array>
<string>10.png</string>
<string>Are you around?</string>
</array>
<key>Kenny Jonnes</key>
<array>
<string>6.png</string>
<string>About our discussion.</string>
</array>
</dict>
</plist>
然后,我在我的视图controller.m
中添加此代码,以便填充。
@interface ViewController ()
{
NSArray *content;
}
-(NSArray *)content
{
if (!_content) {
_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"]];
}
return _content;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.content count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"item 0"];
cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"item 1"];
return cell;
}
我没有得到任何输出显示我有错误。
实际上,我需要在表格视图日期中的主页面看起来像在此图像中一样。
答案 0 :(得分:0)
因为plist中的根节点是<dict>
。
更新
的ViewController:
- (NSArray *)content {
if (!_content) {
_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"]];
}
return _content;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.content count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"image"];
cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"text"];
return cell;
}
Property List.plist:
<plist version="1.0">
<array>
<dict>
<key>image</key>
<string>8.png</string>
<key>text</key>
<string>Hey dude!</string>
</dict>
<dict>
<key>image</key>
<string>7.png</string>
<key>text</key>
<string>Lets have a coffe!</string>
</dict>
<dict>
<key>image</key>
<string>10.png</string>
<key>text</key>
<string>Are you around?</string>
</dict>
<dict>
<key>image</key>
<string>6.png</string>
<key>text</key>
<string>About our discussion.</string>
</dict>
</array>
</plist>
更新
据我所知,“如何使用字典(代码)代替Plist来完成任务”,我修改了代码。
<plist version="1.0">
<dict>
<key>Robert Mitchall</key>
<array>
<string>8.png</string>
<string>Hey dude!</string>
</array>
<key>Carrie Ronald</key>
<array>
<string>7.png</string>
<string>Lets have a coffe!</string>
</array>
<key>Alicia Donnoven</key>
<array>
<string>10.png</string>
<string>Are you around?</string>
</array>
<key>Kenny Jonnes</key>
<array>
<string>6.png</string>
<string>About our discussion.</string>
</array>
</dict>
</plist>
- (NSDictionary *)content {
if (!_content) {
_content = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"]];
}
return _content;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.content count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.content.allKeys objectAtIndex:indexPath.row];
NSArray *detailArray = [self.content.allValues objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [detailArray objectAtIndex:1];
return cell;
}