Adicionando novas linhas para UITableViewCell
-
18-09-2019 - |
Pergunta
Na minha aplicação, eu vou estar exibindo apenas uma linha na UITableView
inicialmente. Eu quero aumentar as linhas como cargas de usuário da linha anterior com os dados (um UIImage, aqui). Como agora estou retornando valor 1, no método numberOfRowsInSection:
, desde que eu não sei como implementá-lo da maneira necessária. Por favor, ajuda.
Meu método cellForRowAtIndexPath:
é
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier ] autorelease];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in nib){
if ([currentObject isKindOfClass:[CustomCell class]]){
cell = (CustomCell *)currentObject;
cell.viewController = self;
break;
}
}
}
if (j<15){
cell.imageView.image = nil;
if (count!=0)
{
@try{
NSInteger row = [indexPath row];
cell.imageView.image = [array objectAtIndex:row];
}
@catch (NSException* ex) {
NSLog(@"doSomethingFancy failed: %@",ex);
}
}
}
cell.showsReorderControl = YES;
return cell;
[array release];
}
que se a condição ea contagem é nada, mas apenas para verificar o correto funcionamento da matriz mutável, 'array'.
Solução
Você deve ler esta documentação: http : //developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/Introduction/Introduction.html
Tenha um olhar para as amostras relacionadas também.
Nota:. Em seu código [liberação arranjo] não será chamado uma vez que você tem uma instrução de retorno antes que ele
Outras dicas
Quando você chegar ao cellForRowAtIndexPath
em um determinado indexPath
, isso significa que essa linha está sendo exibido. Se você chegar à última linha que você pode inserir uma nova linha na tabela. Para fazer isso, você só precisa aumentar a contagem que vai ser retornado pela função tableView:numberOfRowsInSection
.
Eu estou supondo que sua função numberOfRowsInSection
será algo parecido com isto:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return section==0 ? [array count] : 0;
}
Então, basicamente, cellForRowAtIndexPath dentro, adicione o seguinte código:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if([indexPath row]==[array count]-1) {
[array addObject:(NEXT_IMAGE)];
}
return cell;
}
Você também precisa atualizar a tabela. Uma maneira é apenas chamar reloadData em seu UITableView, mas a forma preferida é chamar beginUpdates / insertRowsAtIndexPaths / endUpdates sobre a mesa para que você possa mostrar uma animação agradável da linha sendo inserida.