【发布时间】:2011-02-07 18:34:27
【问题描述】:
我有一个 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 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,所以我不太了解它们是如何工作的。我按照教程做了很多。如何使用方法填充数据?
【问题讨论】:
-
嘿——关于这个较老的问题,对于谷歌人来说,这里有一个现代方法的指针stackoverflow.com/a/25604378/294884希望它对某人有所帮助
标签: objective-c cocoa-touch ios uitableview uikit