在NSMutableArray中输出读取

时间:2014-05-05 20:46:47

标签: ios objective-c

问题:我已经从Windows Azure读取了值。通过NSLog,我能够看到我的应用程序确实从Azure服务器上的表中读入。但是,显示值已成为一个问题。

情况:到目前为止,我在ViewController.m文件中有一个NSMutableArray对象。我已经访问了数组并且能够将表中读取结果的值(在windows azure中)分配给mutableArray。我的问题是我试图通过tableview显示它,但是没有任何显示,当我向下移动表视图时,应用程序崩溃。

我认为主要问题是这一行:

cell.textLabel.text = [clubs objectAtIndex:indexPath.row];

这是ViewController.m代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController{
    NSDictionary *courseDetails;
    NSArray *justCourseNames;

    NSDictionary *webcourseDetails;
    NSArray *webjustCourseNames;

    NSDictionary *clubNames;
    NSArray *location;

    NSMutableArray *clubs;
    NSInteger amount;



}

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

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0)
    {
        return @"Milton Keynes";
    }
    else{
        return @"Stafford";
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0)
    {
        return clubs.count;
    }
    else{
        return webcourseDetails.count;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    UIImage *image = [UIImage imageNamed:@"ClubCellImage"];
    [cell.imageView setImage:image];

//removing the line of code below seems to fix the crash. this is the line of code to display the details

cell.textLabel.text = [clubs objectAtIndex:indexPath.row];


    /*if (indexPath.section == 0)
    {
        //cell.textLabel.text = justCourseNames[indexPath.row];
        //cell.detailTextLabel.text = courseDetails[justCourseNames[indexPath.row]];
    }
    else
     {
        cell.textLabel.text = clubs[indexPath.row];
        //cell.textLabel.text = webjustCourseNames[indexPath.row];
        //cell.detailTextLabel.text = webcourseDetails[webjustCourseNames[indexPath.row]];
    }*/


    return cell;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.client = [MSClient clientWithApplicationURLString:@"https://clublocatortimogunmakin.azure-mobile.net/"
                                            applicationKey:@"ecxnaXEfNpeOvwYYgcViJJoumZlZng45"];


    // Do any additional setup after loading the view.
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"courses" withExtension:@"plist"];
    MSTable *itemTable = [_client tableWithName:@"Item"];

    courseDetails = [NSDictionary dictionaryWithContentsOfURL:url];
    justCourseNames = courseDetails.allKeys;

    NSURL *weburl = [[NSBundle mainBundle] URLForResource:@"courses_web" withExtension:@"plist"];

    webcourseDetails = [NSDictionary dictionaryWithContentsOfURL:weburl];
    webjustCourseNames = courseDetails.allKeys;

    [itemTable readWithCompletion:^(NSArray *results, NSInteger totalCount, NSError *error) {
        clubs = [results mutableCopy];
         amount = totalCount;
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            //NSLog(@"Item read, id: %@", [results objectAtIndex:1]);
            for (int i = 0; i < results.count; i++)
            {
                NSLog(@"Item read, id: %@", [results objectAtIndex:i]);
            }
        }
    }];


}

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

@end

2 个答案:

答案 0 :(得分:1)

当您实现所需的tableview数据源方法(即numberOfRowsInSection和cellForRowAtIndexPath方法)时,您自相矛盾

您可以在此处提供单元格数量:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0)
    {
        return clubs.count;
    }
    else{
        return webcourseDetails.count;
    }

}

因此,您的cellForRowAtIndexPath方法应如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    // cell init/dequeuing 



    if (indexPath.section == 0) {
        cell.textLabel.text = clubs[indexPath.row]; //Assuming that is an NSString instance
    } else {
        cell.textLabel.text = webcourseDetails[indexPath.row]; //Assuming that is an NSString instance
    }


    return cell;
}

答案 1 :(得分:0)

请尝试执行以下操作(我认为您在阅读完所有项目后并未重新加载表格):

[itemTable readWithCompletion:^(NSArray *results, NSInteger totalCount, NSError *error) {
    clubs = [results mutableCopy];
     amount = totalCount;
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        //NSLog(@"Item read, id: %@", [results objectAtIndex:1]);
        for (int i = 0; i < results.count; i++)
        {
            NSLog(@"Item read, id: %@", [results objectAtIndex:i]);
        }
        [YOURTABLENAMEHERE reloadData];
    }
}];

YOURTABLENAMEHERE 替换为对您的表格的引用。