Objective c 持续填充tableview
我有一个UITableView,我想通过计时器调用的方法更新它。我现在得到的是:Objective c 持续填充tableview,objective-c,cocoa-touch,ios,uitableview,uikit,Objective C,Cocoa Touch,Ios,Uitableview,Uikit,我有一个UITableView,我想通过计时器调用的方法更新它。我现在得到的是: -(void) populateTable { NSLog(@"done"); NSArray *array = [[NSArray alloc] initWithObjects:@"ob 1",@"ob 2",@"ob 3",nil]; self.listData = array; [array release]; } - (void)viewDidLoad { [NSTimer scheduledTimer
-(void) populateTable {
NSLog(@"done");
NSArray *array = [[NSArray alloc] initWithObjects:@"ob 1",@"ob 2",@"ob 3",nil];
self.listData = array;
[array release];
}
- (void)viewDidLoad {
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(populateTable) userInfo:nil repeats:YES];
[super viewDidLoad];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
如果我将数组和
self.listData=array
放入viewDidLoad
中,它会工作,但不会重复。正在调用我的方法populateTable
,但实际上没有将任何内容放入表中。这是我第一次使用UITableView,所以我不太了解它们是如何工作的。我通过遵循一个教程做到了这一点。如何使用方法填充数据?您需要将计时器添加到运行循环中-现在您创建了NSTimer对象,但由于它从未将其放入运行循环中,因此它会触发一次并停止。在您的视图中尝试以下操作:
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(populateTable)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
您需要将计时器添加到运行循环中—现在您创建了NSTimer对象,但由于它从未进入运行循环,因此它会触发一次并停止。在您的视图中尝试以下操作:
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(populateTable)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
您还应该在表上调用reloadData
。您还应该在表上调用reloadData
。您必须通知您查看数组中的新条目
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *indexArray = [NSArray arrayWithObject:indexPath];
差不多
[self.tableView insertRowsAtIndexPaths:indexArray with rownanimation:uitableviewrownanimationfade]代码>
在本例中,indexArray是一个IndexPath数组,它提供了更新数组的位置信息
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *indexArray = [NSArray arrayWithObject:indexPath];
之后,视图将调用tableView:cellForRowAtIndexPath:methode获取对象
顺便说一句,IHMO最好您的模型是一个NSMutableArray
,您可以在其中添加一些内容。
在populateTable
的实现中,您只需始终替换数组
PS:仅调用重新加载的数据
。。。分配新数据源时。重新加载表视图将清除当前状态,包括当前选择。。。(UITableView类参考)
您必须通知视图数组中的新条目
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *indexArray = [NSArray arrayWithObject:indexPath];
差不多
[self.tableView insertRowsAtIndexPaths:indexArray with rownanimation:uitableviewrownanimationfade]代码>
在本例中,indexArray是一个IndexPath数组,它提供了更新数组的位置信息
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *indexArray = [NSArray arrayWithObject:indexPath];
之后,视图将调用tableView:cellForRowAtIndexPath:methode获取对象
顺便说一句,IHMO最好您的模型是一个NSMutableArray
,您可以在其中添加一些内容。
在populateTable
的实现中,您只需始终替换数组
PS:仅调用重新加载的数据
。。。分配新数据源时。重新加载表视图将清除当前状态,包括当前选择。。。(UITableView类参考)
对于后代:[[self tableView]reloadData]代码>为后代:[[self tableView]reloadData]代码>嘿--在这个老问题上,对于谷歌来说,这里有一个指向现代方法的指针希望它能帮助某人嘿--在这个老问题上,对于谷歌来说,这里有一个指向现代方法的指针希望它能帮助某人