2011-03-28 78 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只是改變了信息在需要它將工作。我也爲你添加了字幕。

相關問題