我有一个对象数组,我使用以下代码进入tableview
[source addObjectsFromArray:[UDdelegate naturArray]];
[[self tableView]reloadData];
我的应用程序获取有关位置更改的一些数据,该数据是我的naturArray中的对象。 source是列表的数据源。
我的问题是,如果我的列表视图在获取数据时处于活动状态,则列表不会更新。如果我转到主菜单并返回列表,则会显示数据。
有没有办法让列表在活动时更新?
感谢。
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;
}
获取位置时,将从AppDelegate.m
调用updateList方法答案 0 :(得分:0)
原来只是我有点不耐烦了。 [_tabel reloadData]方法只是一点点&#34;重&#34;要调用,所以列表实际上已更新,但只需要一段时间来更新。