2 tipos diferentes de UITableViewCells personalizados em UITableView
-
05-07-2019 - |
Pergunta
no meu UITableView eu quero set pela primeira notícia de um rss feed um costume tableViewCell (tipo A permite dizer) e para o outro segundo notícias, terceiro etc .. outro costume tableViewCell (trype B) o problema é que o personalizado tableViewCell (trype a) criado para a primeira notícia é reutilizado, mas curiosamente o número de linhas entre o primeiro uso do customViewCell (tipo a) ea segunda aparência do mesmo tipo de customViewCell não é igual ..
minha cellForRowAtIndexPath parece que isso.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];
static NSString *CellIdentifier = @"Cell";
if(feedIndex == 0){
MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[[[cell subviews] objectAtIndex:0] setTag:111];
}
cell.feed = item;
return cell;
}
else{
NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier orientation:currentOrientation] autorelease];
[[[cell subviews] objectAtIndex:0] setTag:111];
}
cell.feed = item;
return cell;
}
return nil;
}
os dois tipos de células têm diferentes alturas que está configurado corretamente. alguém poderia me aponte na direção certa sobre como fazer a célula Um costume tipo a aparecer apenas para o primeiro notícias (não sendo reutilizado)? obrigado
Solução
Você deve criar um identificador de célula diferente para os dois estilos de célula:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];
static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";
if(feedIndex == 0) {
MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];
[[[cell subviews] objectAtIndex:0] setTag:111];
}
cell.feed = item;
return cell;
}
else {
NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2 orientation:currentOrientation] autorelease];
[[[cell subviews] objectAtIndex:0] setTag:111];
}
cell.feed = item;
return cell;
}
return nil;
}
Outras dicas
Eu não compreender inteiramente sua pergunta, mas notei duas coisas curiosas. Se você estiver usando dois tipos de células diferentes que você precisa usar dois identificadores celulares distintas ao chamar 'dequeueReusableCellWithIdentifier'. Você está usando o mesmo identificador para ambos, o que é incorreto. Tente algo como:
static NSString *MainArticleIdentifier = @"MainArticle";
static NSString *NewsIdentifier = @"News";
Além disso, eu nunca vi nada parecido com:
int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
Por que não usar:
int feedIndex = indexPath.row;
em cellForRowAtIndexPath
if ("Condition for cell 1") {
cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell in .xib"];
if (cellV == nil) {
[[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL-FILENAME" owner:self options:nil];
cellV = "OUTLET-CEll-IN-VC";
}
} else {
cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell2 in .xib"];
if (cellV == nil) {
[[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL2-FILENAME" owner:self options:nil];
cellV = "OUTLET-CEll-IN-VC";
}
}
[self configureCell:cellV indexpath:indexPath withClipVo:clip];
return cellV;