Mutable Array有时适用于UITableView中的图像

时间:2014-02-14 12:21:09

标签: ios objective-c arrays uitableview

我在使用LogoURL时遇到了一个非常奇怪的阵列问题。有时它会起作用,有时在尝试将数组中的URL显示为图像时会出错。这是一个Mutable数组问题,它让我发疯。 导入“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://mobile.net/" applicationKey:@""];
    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 insertObject:finalstring atIndex:a];
                                                [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"];


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


    return cell;
}



@end

我需要一些新鲜的眼睛,有人可以建议吗? 谢谢

1 个答案:

答案 0 :(得分:0)

我对MSQuery了解不多,但是如果完成块可以在一个单独的线程中,那么这会让你感到困惑。

当你有一些行数时,tableView可以调用-tableView:numberOfRowsInSection:,然后当数字完全不同时调用-tableView:cellForRowAtIndexPath:

另外我不知道MSClient是什么,但是如果它在后台线程中运行它的完成块你正在后台线程中重新加载你的tableView,那么它也会杀了你。