2014-01-06 4 views
0

iOS 개발을 처음 사용합니다. 현재, .plist 파일에서 데이터를 가져와 UITableView에 표시하려고합니다. 응용 프로그램을 실행하는 동안, 엑스 코드는 오류를 제공하지 않았다,하지만 내 응용 프로그램은 아무 것도 보여주지 그러나이iOS는 .plist 파일에서 데이터를로드합니다.

enter image description here

여기

#import <UIKit/UIKit.h> 

@interface TestAppTableViewController : UITableViewController 

@property (nonatomic,strong) NSArray *content; 

@end 

TestAppTableViewController.h 내 코드

입니다 TestAppTableViewController.m

#import "TestAppTableViewController.h" 

@interface TestAppTableViewController() 

@end 

@implementation TestAppTableViewController 

@synthesize content = _content; 

- (NSArray *) content{ 
    if (!_content) { 
     _content = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]]; 
    } 

    return _content; 
} 

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

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    // Return the number of sections. 
    return 0; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [self.content count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 

    cell.textLabel.text = [[self.content objectAtIndex:indexPath.row]valueForKey:@"city"]; 
    cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row]valueForKey:@"state"]; 
    return cell; 
} 

Data.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>city</key> 
     <string>New York</string> 
     <key>state</key> 
     <string>NY</string> 
     <key>cityText</key> 
     <string>This is the description of the city that goes in the tex view just testing.</string> 
    </dict> 
    <dict> 
     <key>city</key> 
     <string>Los Angeles</string> 
     <key>state</key> 
     <string>CA</string> 
     <key>cityText</key> 
     <string>This the second item&apos;s textview</string> 
    </dict> 
    <dict> 
     <key>city</key> 
     <string>Chicago</string> 
     <key>state</key> 
     <string>IL</string> 
     <key>cityText</key> 
     <string>here is the text view description for the third item.</string> 
    </dict> 
    <dict> 
     <key>city</key> 
     <string>Sandiago</string> 
     <key>state</key> 
     <string>CA</string> 
     <key>cityText</key> 
     <string>Here is another city from california</string> 
    </dict> 
    <dict> 
     <key>city</key> 
     <string>Talahasy</string> 
     <key>state</key> 
     <string>FL</string> 
     <key>cityText</key> 
     <string>Talahasy is the capital of the florida state.</string> 
    </dict> 
    <dict> 
     <key>city</key> 
     <string>Boise</string> 
     <key>state</key> 
     <string>Idaho</string> 
     <key>cityText</key> 
     <string>boise is the capital of idaho and i have no idea where that is.</string> 
    </dict> 
</array> 
</plist> 

나는 문제를 알아낼 수하지 않았다. 그래서, 내 질문은 내 코드의 문제는 무엇입니까? 어떻게 plist 파일에서 데이터를 표시 할 수 있습니까? 나는 이전 질문을 stackoverflow에서 찾았지만 그게 도움이되지 못했습니다.

+0

numberOfSectionsInTableView 0을 반환하고 있습니까? – Bamsworld

+2

@ Yasir plist 데이터를 구문 분석 한 다음 tableview에로드해야합니다. 어레이에 정보가 있는지 한 번만 확인하십시오. Json 파서 사용 –

답변

4

귀하의 tableview이

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 

    // Return the number of sections. 
    return 1;// make this 1 
} 

대신 objectForKey 사용하려고 섹션 - 적어도 하나를 필요 이것은 (비록 키가 문서에 따라 @로 시작하지 않으면 자동으로 호출되지만) cast를 입력하면됩니다.

cell.textLabel.text = (NSString*)[[self.content objectAtIndex:indexPath.row] objectForKey:@"city"]; 
cell.detailTextLabel.text = (NSString*)[[self.content objectAtIndex:indexPath.row] objectForKey:@"state"]; 

아직 아무것도 얻지 못했다면 내용에 실제로 plist 데이터가 포함되어 있는지 확인할 가치가 있습니다. - (NSArray *) 콘텐츠 메서드에서 반환하기 전에 _content의 값을 기록 할 가치가 있습니다. 또한 당신은 당신이 사전을 사용할 수있는 배열의 내부에있는 당신의 PLIST를 참조 self.content = _content

1

나는 당신이 1 반환해야합니다, 문제는 여기에 생각이나 숫자는 0을 제외하고 원하는 :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    // Return the number of sections. 
    return 0; 
} 
0
@interface ViewController:UIViewController <UITableViewDelegate, UITableDataSource> 

확인합니다. 그래서 아래 코드를 구현하면 도움이됩니다.

- (void)viewDidLoad { 

    tableView.delegate = self; 
    tableView.dataSource = self; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"yourFilename" ofType:@"plist"]; 
    NSArray *contentArray = [NSArray arrayWithContentsOfFile:path]; 
    // Having outlet for tableArray variable. 
    tableArray = [[NSMutableArray alloc]initWithArray:contentArray copyItems:YES]; 

    [super viewDidLoad]; 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [tableArray count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // In your case dictionary contains strings with keys and values. The below line returns dictionary only. not array.. 
    NSDictionary *dictionary = [tableArray objectAtIndex:section]; 
    return dictionary.allKeys.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier]; 
    } 
    NSDictionary *dictionaryOfArray = [tableArray objectAtIndex:indexPath.section]; 
    NSArray *keysArray = dictionaryOfArray.allKeys; 
    cell.textLabel.text = [keysArray objectAtIndex:indexPath.row]; 
    cell.detailTextLabel.text = [dictionaryOfArray valueForKey:[keysArray objectAtIndex:indexPath.row]]; 
    return cell; 
}