I’m trying to create a custom UITableView with different cell identifiers. In the first cell should display an image and below to follow the rest of the cells. However when after scrolling the displayed image disappears. I tried to solve by looking for the answers provided by others for similar problems but without success.
Here’s the code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
EventoCell *cell;
static NSMutableString *CellIdentifier;
if(i==0){
CellIdentifier = @"imgCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
i++;
}
else{
CellIdentifier = @"CellaEvento";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.Nome.text=[[AShow objectAtIndex:indexPath.row]GetEvento];
cell.Localita.text=[[AShow objectAtIndex:indexPath.row]GetLocalita];
cell.Ora.text=[NSString stringWithFormat:@"%d:%d",[[AShow objectAtIndex:indexPath.row]GetOra],[[AShow objectAtIndex:indexPath.row]GetMinuti]];
[cell setValue:[[AShow objectAtIndex:indexPath.row]GetEvento] :[[AShow objectAtIndex:indexPath.row]GetGiorno] :[[AShow objectAtIndex:indexPath.row]GetMese] :[[AShow objectAtIndex:indexPath.row]GetAnno] :[[AShow objectAtIndex:indexPath.row]GetOra] :[[AShow objectAtIndex:indexPath.row]GetMinuti]];
}
return cell;
}
Do you want to display image in the first row? If so, I think you can change the line for judge whether it is the first row from
to
I think the i must be a class member. The
UITableViewonly creates limited number ofUITableViewCell. Commonly, the number equals to to the number of displaying rows. For example, if the screen can only display 10 rows,UITableViewcreates 10 cell. After scrolling, It reuses the created cells by callingdequeueReusableCellWithIdentifier:forIndexPath, this makes the rows which are out of screen release their cells. When scroll back, each “new” entered item needs a cell. TheUITableViewwill calltableView:cellForRowAtIndexPathfor new cell. Therefore as your scenario, only the first time, the image row can be displayed, because after the first time, the i is non-zero even scroll back.