2011-02-07 7 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

안녕을 포함하여 현재 상태를 지 웁니다 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 단지

를 호출합니다. 여기 Google 직원이 오래된 질문에 현대적인 접근 방식 http://stackoverflow.com/에 대한 포인터입니다 - 테이블 뷰를 다시로드하면 현재 선택 (jQuery과 클래스 참조)

관련 문제