NSMutableArray问题

时间:2014-02-14 11:18:00

标签: ios objective-c arrays uitableview

全部,

我的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)错误,因为它无法看到对象。任何帮助都会很棒,谢谢。

2 个答案:

答案 0 :(得分:0)

因为你的for循环覆盖了数组值而只存储了最后一个值。

[array insertObject:finalString atIndex:i];

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

    return [self.logoURL count];
}

答案 1 :(得分: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];