Hey guys i’ve been stuck on this problem for 2 days in a row, so i’m asking if there is anybody out there that can give me a hand.
I have a tableview that is made up of 4 sections.
section 1 ->made up of just 1 row where its cell contains a subview which is a uiimageview
section 2 ->made up of 2 normal rows(just 2 plain simple cells with text in them)
section 3 -> made up of 1 normal row
section 4 -> made up of 1 row, where its cell contains a subview which is a uitextview that can contain dynamic text, so the uitextview’s height and consequently the cells height varies depending on how much text is in the uitextview.
Here’s the code to create this structure:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//create a nsstring object that we can use as the reuse identifier
static NSString *CellIdentifier = @"Cell";
//check to see if we can reuse a cell from a row that has just rolled off the screen
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if there re no cells that can be reused, create a new cell
if(cell==nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
switch (indexPath.section) {
case 0:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:_viewForImageHeader];
break;
case 1:
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = 0;
cell.textLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:16.0];
break;
case 2:
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:16.0];
break;
default:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:_textViewForArticle];
break;
}
}
else{
NSLog(@"in else");
}
//here i fill in the 2 normal cells with text
return cell;
}
When the uitableview loads(in portrait mode) everything is perfect(image is in section 1, section 2 and 3 contain their correct text and in section 4 i have my dynamic text). But when i start to rotate the app, all the cells get mixed up. For example i find the contents of section3 in section 4 and vice versa.
I think this has to with the fact that i am not maybe reusing the cells correctly. Should i use tags, and if so, how can i implement the use of tags in the specific case?
Put
switchcase outsideifcondition – after theif and else