2011-03-28 73 views
1

中,读取PList并在tableView控制器中设置cell.textLabel.text只是一个非常愚蠢的问题。从PList获取单元格标题的问题在UITableView

我的plist看起来是这样的:

<?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>Rows</key> 
     <array> 
      <dict> 
       <key>Name</key> 
       <string>Big Ben</string> 
      </dict> 
      <dict> 
       <key>Name</key> 
       <string>Westminster Abbey</string> 
      </dict> 
     </array> 
     <key>Title</key> 
     <string>London</string> 
    </dict> 
    <dict> 
     <key>Rows</key> 
     <array> 
      <dict> 
       <key>Name</key> 
       <string>Eiffel Tower</string> 
      </dict> 
     </array> 
     <key>Title</key> 
     <string>Paris</string> 
    </dict> 
</array> 
</plist> 

,我只是试图让名字进入细胞。这是我的目标。

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

在此先感谢。

+0

如果您将该行分成多个语句,您将更容易阅读和调试。您可以一次一步或单步测试一件事,看看发生了什么。 – Anna 2011-03-28 21:14:44

+0

感谢您的建议 - 将做到这一点。 – 2011-03-28 21:33:05

回答

1

感谢@aBitObious的建议 - got是排序的。答案是:

NSDictionary *dictionary = [[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"]; 
cell.textLabel.text = [[dictionary objectAtIndex: indexPath.row] objectForKey: @"Name"]; 
1

试试这个:

.M

#import "plistTableViewController.h" 
#import "DetailViewController.h" 
@interface plistTableViewController() 

@end 

@implementation plistTableViewController 


- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 

    NSString *mylist = [[NSBundle mainBundle] pathForResource:@"lodges" 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; 
} 

.H

#import <UIKit/UIKit.h> 

@interface plistTableViewController : UITableViewController 
{ 
    NSArray *tabledata; 
} 

@end 

我的plist名为lodges.plist只是改变了信息在需要它将工作。我也为你添加了字幕。