使用NSMutableArray填充TableView的问题

时间:2012-12-28 20:38:02

标签: objective-c cocoa-touch uitableview

我使用SudzC作为iPhone应用程序。我成功地调用了我的ASP.Net webservice并将所需的数据拉入名为tableData的NSMutable数组中。我有一个tableView应该显示tableData的内容;但这不起作用。我在这个问题上找了好几个小时。我对Objective C世界非常陌生,所以我认为这对我来说是一个小小的疏忽。我也将我的TableView链接到ViewController(委托/数据源)。

这是我的代码:

#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 个答案:

答案 0 :(得分:1)

确保在.h中正确设置了委托和数据源:

@interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

如果您将XIB用于用户界面,则tableview将作为数据源和委托连接到文件所有者。 enter image description here

- 编辑: 为您创建一个Outlet tableview: enter image description here