2012-12-28 43 views
0

我使用SudzC作爲iPhone應用程序。我成功地調用了我的ASP.Net webservice,並將所需的數據拖入一個名爲tableData的NSMutable數組中。我有一個tableView應該顯示tableData的內容;但是這不起作用。我在這個問題上找了幾個小時。我對Objective C世界非常陌生,所以我認爲這是我的一個小疏忽。我也將TableView鏈接到ViewController(委託/數據源)。使用NSMutableArray填充TableView的問題

這裏是我的代碼:

#import "ViewController.h" 
#import "DocumentServiceJSONService.h" 

@interface ViewController() 

@end 

@implementation ViewController 
NSMutableArray *tableData; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

-(void)viewWillAppear:(BOOL) animated { 
    tableData = [[NSMutableArray alloc] init]; 
    DocumentServiceJSONService* service = [[DocumentServiceJSONService alloc] init]; 
    [service GetAllEMSDocumentsXMLAsString: self action:@selector(handleGetAllEMSDocuments:)]; 
    [super viewWillAppear:animated]; 
} 


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

-(void) handleGetAllEMSDocuments:(id) result { 

    if ([result isKindOfClass:[NSError class]]) { 
     //if an error has occurred, handle it 
     return; 
    } 

    if([result isKindOfClass: [SoapFault class]]) { 
     return; 
    } 

    NSString* xmlString = result; 


    CXMLDocument *xmlDoc = [[CXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil]; 
    NSArray *nodes = NULL; 
    nodes = [xmlDoc nodesForXPath:@"//EMSDocuments" error:nil]; 

    for (CXMLElement *node in nodes) { 
     NSMutableDictionary *item = [[NSMutableDictionary alloc] init]; 
     int counter; 

     for (counter = 0; counter < [node childCount]; counter++) { 
     [item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]]; 
     } 
    //[item setObject:[[node attributeForName:@"DisplayName"] stringValue] forKey:@"DisplayName"]; 
     NSString *displayName = [item objectForKey:@"DisplayName"]; 
     [tableData addObject:displayName]; 
    } 


} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [tableData count]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 
    return cell; 
} 

@end 

任何建議表示讚賞。

回答

1

確保您在.H有委託和數據源設置正確:

@interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

如果您正在使用XIBs用戶界面,即實現代碼如下連接到文件的所有者作爲數據源和委託。 enter image description here

- 編輯: 創建奧特萊斯爲你實現代碼如下: enter image description here

+0

謝謝你的建議,我有這兩個設置的。我在我的應用程序中使用StoryBoard,並指定了TableView數據源和委託給ViewController。 – mint

+0

您是否在任何tableview函數內部插入了NSLog以查看它們是否被調用? – gmogames

+0

我已經設置了一些斷點,看起來我的tableData計數在numberOfRowsInSection方法中返回0 ...我的tableData計數是早些時候5,但似乎在填充tableData之前調用此方法。我需要在其他地方調用我的方法調用嗎? – mint