2011-02-07 12 views
0

私はタイマーによって呼び出されるメソッドによって更新したい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; 

} 

私はviewDidLoadにアレイとself.listData = arrayを置けば、それは動作しますが、繰り返すことはしません。私のメソッドpopulateTableが呼び出されていますが、実際には何もテーブルに入れていません。これは初めてのUITableViewを使用しているので、どのように動作するか分かりません。私はチュートリアルに従うことでこれを多くしました。メソッドを使用してデータを生成するにはどうすればよいですか?

+0

こんにちは、この古い質問では、近代的なアプローチへのポインタhttp://stackoverflow.com/ a/25604378/294884誰かを助けることを願って – Fattie

答えて

3

あなたはまた、あなたのテーブルの上にreloadDataを呼び出す必要があります。

+0

どうすればいいですか? – Chris

+0

心配しないでください!理解した。できます! – Chris

+1

後継の場合: '[[self tableView] reloadData];' – winsmith

0

実行ループにタイマーを追加する必要があります。今はNSTimerオブジェクトを作成しますが、実行ループには決して入れないので、一度起動して停止します。あなたのviewDidLoadにこのような何かを試してみてください:

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:2.0 
                target:self 
               selector:@selector(populateTable) 
               userInfo:nil 
                repeats:YES]; 
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
0

アレイ内の新しいエントリについての情報を通知する必要があります。

この場合

[self.tableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];

ような何かindexArrayあなたは、配列を更新された情報を提供しますIndexPathsの配列です。

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
NSArray *indexArray = [NSArray arrayWithObject:indexPath]; 

ビュービューは、あなたにtableViewを呼び出します:cellForRowAtIndexPath:メソッドは、オブジェクトを取得します。

btw。 IHMOそれはあなたがモデルを追加することができますNSMutableArrayが良いです。 populateTableの実装では、常に配列を置き換えます。

PS:あなたは新しいデータソースを割り当てるとき... reloadDataのみ

を呼び出します。テーブルビューを再読み込みすると、現在の選択を含む現在の状態がクリアされます。(UITableViewクラスリファレンス)

関連する問題