2014-02-14 135 views
0

全部,NSMutable陣列問題

我有我的NSMutable數組LogoURL的問題。當UITable重新加載時,它只顯示在數組位置[0]而不是[1]或[2]。這是我的代碼,有人可以看看我哪裏出錯了。它非常小的問題,但它讓我瘋狂!

#import "ViewController.h" 

@interface ViewController() 
@property (nonatomic, strong) MSTable *table; 
@property (nonatomic, strong) NSMutableArray *items; 



@property (weak, nonatomic) IBOutlet UITableView *MainTableView; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 


    [super viewDidLoad]; 
    // create the activity indicator in the main queue 
    self.MainTableView.hidden = YES; 

    UIActivityIndicatorView *ac = [[UIActivityIndicatorView alloc] 
            initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    [self.view addSubview:ac]; 
    [ac startAnimating]; 






    self.client = [MSClient clientWithApplicationURLString:@"https://outnight-mobile.azure-mobile.net/" applicationKey:@"okYeRGfBagYrsbkaqWIRObeDtktjkF10"]; 
    self.table = [self.client tableWithName:@"notifications"]; 
    self.logoURL = [[NSMutableArray alloc] init]; 
    self.rowitems = [[NSMutableArray alloc] init]; 
    MSQuery *query = [self.table query]; 
    query.fetchLimit = 3; 
    [query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error) 
           { 

            self.rowitems = [items mutableCopy]; 
            //[self.MainTableView reloadData]; 




            int a; 
            for (a = 0; a < 3; a++) 
            { 
             NSDictionary *apt = [self.rowitems objectAtIndex:a]; 
             NSLog(@"%@", apt[@"barID"]); 
             NSDictionary *barIDDictionary = @{ @"myParam": apt[@"barID"]}; 
             self.client = [MSClient clientWithApplicationURLString:@"https://outnight-mobile.azure-mobile.net/" applicationKey:@"okYeRGfBagYrsbkaqWIRObeDtktjkF10"]; 
             [self.client invokeAPI:@"photos" body:barIDDictionary HTTPMethod:@"POST" parameters:nil headers:nil completion:^(id result, NSHTTPURLResponse *response, NSError *error) { 


              if (error) { 
                 NSLog(@"Error %@", error); 
              } 
              else  { 
               NSString *string = [NSString stringWithFormat:@"%@", [result objectForKey:@"rows"]]; 
               NSString *stringWithoutbracketsend = [string stringByReplacingOccurrencesOfString:@")" withString:@""]; 
               NSString *stringWithoutbracketsfront = [stringWithoutbracketsend stringByReplacingOccurrencesOfString:@"(" withString:@""]; 
               NSString *completion = [stringWithoutbracketsfront stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
               NSString *newStr = [completion substringFromIndex:1]; 
               NSString *finalstring = [newStr substringToIndex:newStr.length-(newStr.length>0)]; 
               [self.logoURL addObject:finalstring]; 
               [self.MainTableView reloadData]; 
              } 




              }]; 

            } 



           }]; 

           self.MainTableView.hidden = NO; 


} 


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


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

    return [self.rowitems count]; 
} 


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    NSDictionary *stress = [self.rowitems objectAtIndex:indexPath.row]; 
    cell.textLabel.text = stress[@"content"]; 

    switch (indexPath.row) { 
     case 0: 
      [cell.imageView setImageWithURL:[NSURL URLWithString:[self.logoURL objectAtIndex:(0)]] placeholderImage:[UIImage imageNamed:@"greybox40.png"]]; 

      break; 

     case 1: 
      [cell.imageView setImageWithURL:[NSURL URLWithString:[self.logoURL objectAtIndex:(1)]] placeholderImage:[UIImage imageNamed:@"greybox40.png"]]; 

      break; 

     case 2: 
      //[cell.imageView setImageWithURL:[NSURL URLWithString:[self.logoURL objectAtIndex:(2)]] placeholderImage:[UIImage imageNamed:@"greybox40.png"]]; 
      break; 

    } 


    return cell; 
} 



@end 

它是在示數CASE(1),COS它不能看到的對象。任何幫助將是輝煌的,謝謝。

+0

你調試中有多少項目您的陣列(收費)? – Wain

+0

我做了一個斷點是的,但似乎沒有添加它們 - 我不這麼認爲。我可以用來調試這個簡單的錯誤的任何其他工具? – user3307144

+0

您應該將viewDidLoad方法中的所有代碼替換爲init方法,因爲它與UI沒有關係。 – Miroslav

回答

0

發生情況(1)時發生錯誤,因爲您重新加載了3次表格視圖。並且每次通過numberOfRowsInSection:方法向您的tableView表示它有3行時。但在第一次和第二次迭代中,您的self.logoURL只有1個,然後是2個對象。試試這個代碼:

if (self.logoURL.count > indexPath.row) { 
     [cell.imageView setImageWithURL:[NSURL URLWithString:self.logoURL[indexPath.row]] placeholderImage:[UIImage imageNamed:@"greybox40.png"]]; 
    } 
    else 
     cell.imageView.image = nil; 

也,你不需要創建NSMutableArray的實例self.rowitems = [[NSMutableArray alloc] init];因爲你再重新分配self.rowitems財產self.rowitems = [items mutableCopy];

+0

我認爲你可能已經破解它!讓我改變網址,我可能會有一個很好的結果.. – user3307144

+0

它仍然不完美,因爲它有時工作,其他時候它錯誤。它需要更多的工作。當我確認圖像時,我會重新發布代碼,謝謝。 – user3307144

+0

我認爲你應該改變'(a = 0; a <3; a ++)NSDictionary * apt = [self.rowitems objectAtIndex:a];'爲'(NSDictionary * apt in self.rowitems)',這會減少一個錯誤 –

0

因爲您的for循環覆蓋數組值並僅存儲最後一個值。

[array insertObject:finalString atIndex:i]; 

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

    return [self.logoURL count]; 
} 
+0

好吧,這是有道理的。什麼是所有的項目有點意思?我的意思是我應該在那裏放置什麼? – user3307144

+0

您的數據....... – codercat

+0

索引1超越界限爲空陣列 - 再次相同 – user3307144