UITableView的问题 - 请指教

时间:2014-02-13 17:33:13

标签: ios objective-c uitableview

我有这段代码,它应该带一个ActivityIndi​​cator(很好),然后两次查询数据库(查看它带来的所有正确信息的日志)。我只是仍然没有出现在tableview上,它让我发疯!如果可能的话,你可以查看代码,看看有什么不对 - 我已经在这几天了 - 请帮忙!

 [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.rowitems = [[NSMutableArray alloc] init];
    MSQuery *query = [self.table query];
    query.fetchLimit = 3;
    [query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error)
                                {

                                                self.rowitems = [items mutableCopy];
                                                // we have the notifications and the barID in ROWITEMS array
                                                // so lets make this into a dictionary so we can query it
                                                // we need to run this next block of code 3 times, for the 3 bar ids we have got in ROWITEMS
                                                //[self.TableView 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];
                                                NSLog(@"%@",finalstring);
                                             }

                                            }];




                                  }
                                }];

    [self.MainTableView reloadData];
    self.MainTableView.hidden = YES;
}

这是我的所有代码谢谢

#import "ViewController.h"

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


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

@end

@implementation ViewController {
    dispatch_queue_t MyQueue;
}


- (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.rowitems = [[NSMutableArray alloc] init];
    MSQuery *query = [self.table query];
    query.fetchLimit = 3;
    [query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error)
                                {

                                                self.rowitems = [items mutableCopy];
                                                // we have the notifications and the barID in ROWITEMS array
                                                // so lets make this into a dictionary so we can query it
                                                // we need to run this next block of code 3 times, for the 3 bar ids we have got in ROWITEMS
                                                [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];
                                                NSLog(@"%@",finalstring);
                                                [ac stopAnimating];
                                                [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 3;
}


-(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"];






    // putting an image on a row

    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

3 个答案:

答案 0 :(得分:0)

看起来你需要打电话

[self.MainTableView reloadData];
self.MainTableView.hidden = NO;

Completion块内

答案 1 :(得分:0)

您是否实施了UITableViewDatasourceUITableViewDelegate协议?

实施此2协议所需的方法并链接您的MainTableView

编程:

MainTableView.datasource = self;
MainTableView.delegate = self;

通过故事板:

enter image description here

enter image description here

<强> UPD:

试试这段代码:

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
NSDictionary *stress = [self.rowitems objectAtIndex:indexPath.row];

cell.textLabel.text = [stress objectForKey:@"content"];

答案 2 :(得分:0)

我认为您的查询是异步运行的。您需要做的就是将[tableView reloadData]放入完成查询阻止后运行的块中,并确保将 reloadData 调用放入 dispatch_async 阻止。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Background work
    dispatch_async(dispatch_get_main_queue(), ^{
        // Update UI
        [tableView reloadData];
    }); });