2014-05-05 83 views
-1

問題:我已經從Windows Azure中讀取了值。通過NSLogs,我能夠看到我的應用程序確實從Azure服務器的表中讀入。但是,顯示值已成爲一個問題。在NSMutable陣列中輸出讀取

情況:到目前爲止,我在ViewController.m文件中有一個NSMutableArray對象。我已經訪問了數組,並能夠從表中讀取結果(在windows azure中)賦值給mutableArray。我的問題是,我試圖通過tableview顯示它,但沒有顯示,當我向下移動表視圖時,應用程序崩潰。

相信的主要問題是這一行:

cell.textLabel.text = [俱樂部objectAtIndex:indexPath.row];

請與我裸露,我是新來的Xcode和Objective-C

這裏是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 
+0

你可以添加崩潰日誌嗎? – lucianomarisi

+0

這是你在找什麼?16 CoreFoundation 0x01cb236e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30 –

回答

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; 
} 
+0

感謝您的幫助,現在已經解決了崩潰!但是沒有數據輸出到桌面上呢?我知道100%被閱讀,因爲我有詳細信息打印在日誌中:S的幫助非常感謝 –

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參考您的表。