UITableView не будет обновляться, пока активен - PullRequest
1 голос
/ 01 марта 2012

У меня есть массив объектов, и я использую следующий код для входа в таблицу

[source addObjectsFromArray:[UDdelegate naturArray]];

[[self tableView]reloadData];

Мое приложение извлекает некоторые данные об изменении местоположения, и эти данные являются объектами в моем naturArray.Источник - это источник данных списка.

Моя проблема в том, что, если мое представление списка активно во время выборки данных, список не обновляется.Если я зайду в главное меню и вернусь в listView, появятся данные.

Есть ли способ получить список для обновления, пока он активен?

Спасибо.

Редактировать: Полный код

VisListe.h:
@class UdINaturenAppDelegate;
@class POI;
@class webDetailView;

@interface VisListe :  UITableViewController<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain,readwrite) IBOutlet NSMutableArray *dataSourceArray;

@property (strong, nonatomic) UdINaturenAppDelegate *UDdelegate;

@property (strong, nonatomic) POI *poi;

@property (strong, nonatomic) webDetailView *webView;

@property (strong, nonatomic) IBOutlet UITableView *tabel;

-(void)updateList;

-(NSString*)parseURL:(NSString*)url;
@end

VisListe.m:

@implementation VisListe
@synthesize dataSourceArray = source;
@synthesize UDdelegate;
@synthesize poi=_poi;
@synthesize webView = _webView;
@synthesize tabel = _tabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = NSLocalizedString(@"Poi List", nil);

        UDdelegate = (UdINaturenAppDelegate*) [UIApplication sharedApplication].delegate;
        source = [[NSMutableArray alloc]init];
        _webView = [[webDetailView alloc] initWithNibName:@"webDetailView" bundle:nil];
        [self updateList];

    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}



#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];


    // Do any additional setup after loading the view from its nib.
    //[source addObjectsFromArray:[UDdelegate naturArray]];

}

- (void)viewDidUnload
{

    [self setTabel:nil];
    [super viewDidUnload];

    self.dataSourceArray = nil; // this will release and set to nil
    source = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    NSLog(@"unload");
}
- (void)viewDidAppear:(BOOL)animated
{

    [super viewDidAppear:animated];

    [self updateList];

}

- (void)viewDidDisappear:(BOOL)animated
{

    [super viewDidDisappear:animated];



}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

// to determine specific row height for each cell, override this.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ([indexPath row] == 0) ? 60.0 : 60.0;
}

// to determine which UITableViewCell to be used on a given row.

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

    //return [UDdelegate naturArray].count;
    return [source count];
}
//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    _poi = [source objectAtIndex:indexPath.row];

    cell.textLabel.text = [_poi title];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Afstand: %f m.", [_poi dist]];
  //[cell.imageView setImage:<#(UIImage *)#>];

    return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _poi = [source objectAtIndex:indexPath.row];
    NSLog(@"%@",[_poi title]);

    NSString* u = [self parseURL:[_poi webUrl]];



    [_webView setIncUrl:u];

    [[self navigationController] pushViewController:_webView animated:YES];

}

-(NSString*)parseURL:(NSString*)url{


    NSString* s = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    return s;

}


-(void)updateList{

    [source removeAllObjects];

    [source addObjectsFromArray:[UDdelegate naturArray]];

    [_tabel reloadData];

}

-(UIImage*)setImgFromUrl:(NSString*)url{

    NSURL *newurl = [NSURL URLWithString: url];


    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL: newurl]];

    return image;

}

При получении местоположения вызывается метод updateListиз AppDelegate.m

1 Ответ

0 голосов
/ 23 марта 2012

Оказалось, что только я был немного нетерпеливым.метод [_tabel reloadData] просто немного «тяжелый» для вызова, поэтому список фактически обновился, но для его обновления потребовалось немного времени.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...