Ir para conteúdo
Visualizar no app

Uma forma melhor de navegar. Saiba mais.

MM Fórum

Um app em tela cheia na sua Tela de Início com notificações push, avisos e mais.

Para instalar este app no iOS/iPadOS
  1. Toque no ícone de Compartilhamento no Safari
  2. Role o menu e toque em Adicionar à Tela de Início.
  3. Toque em Adicionar no canto superior direito.
Para instalar este app no Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Custom Table View Cell

Featured Replies

Postado

Ola a todos,

Estou criando uma TableView onde quero que as cells sejam exclusivamente Custom TableViewCells, a partir de um padrao construido no Interface Builder. Criei a cell e no cellForRowAtIndexPath eu faço

return customCell;

Mas a table view nao é totalmente populada. Apenas a ultima cell visivel é populada com o conteudo definido pelo interface builder. Eis o codigo utilizado:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"customCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

	customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (customCell == nil) {
		customCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
	}

	cell.textLabel.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yellowColor.png"]];

    // Configure the cell...
	eventoCellTextLabel.text = @"Evento";

    return customCell;
}

Mas nada aparece. As cells estao la, pois posso ate clicar nelas, mas o conteudo definido no IB nao...

Agradeço muito a quem puder ajudar!

Editado por Gabriel Vincent

  • Respostas 2
  • Visualizações 977
  • Criado
  • Última resposta

Top Postadores Neste Tópico

Postado

faz algo assim ...


//Crie uma tag única para cada objeto que será manipulado.
    static NSUInteger const kNameLabelTag = 2;

    //Declaração das Labels.
    UILabel *nameLabel = nil;

	//Identidade de cada Cell.
    static NSString *CellIdentifier = @"LocationCell";

	//Cria uma Cell com a identidade e atribui ao ponteiro uma celula com a identidade usada.  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

	if (cell == nil) {
        // Não Tem Celulas na tela para reuso, então criamos.
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];

        //Local na celula onde vai ser iniciado o frame.
        nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 3, 190, 20)];
        nameLabel.tag = kNameLabelTag;
        nameLabel.font = [UIFont boldSystemFontOfSize:14];
        nameLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
		//adiciona ao subview.
        [cell.contentView addSubview:nameLabel];
    } else {
        // tem uma célula na tela disponível para reuso
        nameLabel = (UILabel *)[cell.contentView viewWithTag:kNameLabelTag];
    }

	//Recupere o dado da celula que vai exibir …
    Location *managedObject = (Location *)[self.fetchedResultsController objectAtIndexPath:indexPath];

    //atribua o valor ao label custom.
    nameLabel.text = [NSString stringWithFormat:@"%@%@", @"", managedObject.name];

    //Adiciona uma setinha em cada tela pra dizer ao usuário que uma ação pode ser tomada.
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

para muitos labels ou quaisquer objetos na custom cell o melhor é atribuir tags a cada um, assim vc tem como identificar qual objeto é qual quando for atribuir um ponteiro para cada objeto na célula.

obs.: estou usando o ARC do IOS 5 sdk, adicione os autorelease e releases necessários para SDK mais antigos. ;)

Editado por Artur Felipe

  • 2 semanas depois...
Postado

Boa Noite, espero que ajudar a solucionar o seu problema, caso ainda não o tenha. Tenta fazer os seguinte:

Para facilitar crie, além do .xib, o .h e o .m da sua célula.

Escreve o código abaixo na tabela onde queres mostrar a nova célula.


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"nomeDaClasseDaCustomCell";

nomeDaClasseDaCustomCell *cell = (nomeDaClasseDaCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cellItens == nil) {
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"nomeDaClasseDaCustomCell" owner:self options:nil];

cellItens = [nibs objectAtIndex:0];

}
//Configura a tua célula
cell.textLabel.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yellowColor.png"]];
cell.propriedadeLabelDaClasseCuston.text = @"Evento";

return cell;
}
[/CODE]

Também não esquece de implementar o código:

[CODE]

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return valorDoHeightDaCelulaCustonNoInterfaceBuilder;
}
[/CODE]

Outra coisa chata, que as vezes esqueço, é setar no .xib da classe Custon o .h que criastes, assim como o Identifier no Atributes Inspector. E outro fator é o de fazer a conexão dos teus IBOutlets com os Objects e não com o File's Owner.

Espero ter ajudado, boa sorte! ;)

Participe do debate

Você pode postar agora e se registrar depois. Se você tem uma conta, entre agora para postar com ela.

Visitante
Responder este tópico…

Quem Está Navegando 0

  • Nenhum usuário registrado visualizando esta página.

Conta

Navegação

Buscar

Buscar

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.