Eu tenho uma página de histórico, que é um UItableview com 5 linhas. Eu configurei a célula protótipo para as especificações que eu quero e adicionei este texto ao arquivo historyviewcontroller.h correspondente:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryItem"];
return cell;
}
Não vejo nenhuma célula quando executo o aplicativo. Eu claramente perdi alguma coisa, mas não consigo ver o que.
Respostas:
5 para resposta № 1Você precisa realmente criar as células. dequeueReusableCellWithIdentifier recupera apenas as células já criadas e não cria novas.
Veja como fazer isso:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"HistoryItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if cell is not nil, it means it was already created and correctly dequeued.
if (cell == nil) {
//create, via alloc init, your cell here
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}