I have created a UITableViewCell nib that I am loading into my tableview, however something weird is happening when I scroll through the table, if I am scrolling down the new cell that comes in shows the default values for a split second and then if I scroll up the same thing is happening at the top.
The code below is deciding which nib to load depending on the data avalible from myObj, hopefully this is understandable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section == 0) {
// Set cell height
[self tableView:tableView heightForRowAtIndexPath:indexPath];
//call obj array with all of the values from my sorting algorithum
SearchResultItem *myObj = (searchResultItem*)[dataArrayOfObjects objectAtIndex:indexPath.row];
// This gets the year string ready ----->
// Create Year string
NSString *yearRangeString = [[NSString alloc] init];
// do some special stuff here with the years
if ((myObj.yearStart < 1) && (myObj.yearFinish < 1)){
// yearRangeLabel.text = @"";
yearRangeString = @"";
}
if ((myObj.yearStart < 1) && (myObj.yearFinish > 1)){
// yearRangeLabel.text = [NSString stringWithFormat:@"upto %i", myObj.yearFinish];
yearRangeString = [NSString stringWithFormat:@"upto %i", myObj.yearFinish];
}
if ((myObj.yearStart > 1) && (myObj.yearFinish < 1)){
// yearRangeLabel.text = [NSString stringWithFormat:@"from %i", myObj.yearStart];
yearRangeString = [NSString stringWithFormat:@"from %i", myObj.yearStart];
}
if ((myObj.yearStart > 1) && (myObj.yearFinish > 1)){
// yearRangeLabel.text = [NSString stringWithFormat:@"%i to %i",myObj.yearStart, myObj.yearFinish];
yearRangeString = [NSString stringWithFormat:@"%i to %i",myObj.yearStart, myObj.yearFinish];
}
// adjust year string is (submodel) is avalible or not
if (([yearSelectedString isEqualToString:@"all"]) && ([sModelSelectedString isEqualToString:@"all"])) {
yearRangeString = [NSString stringWithFormat:@"(%@) %@", myObj.subModel, yearRangeString];
yearRangeLabel.text = yearRangeString;
}
else if ((![yearSelectedString isEqualToString:@"all"]) && ([sModelSelectedString isEqualToString:@"aSearchResultItemll"])) {
yearRangeString = [NSString stringWithFormat:@"(%@)", myObj.subModel];
yearRangeLabel.text = yearRangeString;
}
else if (([yearSelectedString isEqualToString:@"all"]) && (![sModelSelectedString isEqualToString:@"all"])) {
yearRangeString = [NSString stringWithFormat:@"%@", yearRangeString];
yearRangeLabel.text = yearRangeString;
}
else if ((![yearSelectedString isEqualToString:@"all"]) && (![sModelSelectedString isEqualToString:@"all"])) {
yearRangeLabel.text = nil;
}
// check year string if too large use B nibs ----->
// Get yearRangeString width to see if you need to use a yearRangeLabel with second line
CGSize yearStringSize = [yearRangeString sizeWithFont:[UIFont fontWithName:@"Helvetica" size:15] constrainedToSize:CGSizeMake(226, 21) lineBreakMode:NSLineBreakByWordWrapping ];
// NSLog(@"width = %f, height = %f", yearStringSize.width, yearStringSize.height);
// Get rest of nibs ready ----->
// Set custom cell to display
// check to see if there is a description, if there is choose the correct tableviewcell
if (([myObj.note isEqualToString:@""]) && (([sModelSelectedString isEqualToString:@"all"]) || ([yearSelectedString isEqualToString:@"all"])) && (yearStringSize.width <= 100.00)) {
// Configure the cell using custom cell
[[NSBundle mainBundle] loadNibNamed:@"auSearchCell" owner:self options:nil];
// Set up the different labels in each cell
descriptionLabel.text = myObj.sDescription;
iDLabel.text = [NSString stringWithFormat:@"%i", myObj.mID];
cLabel.text = myObj.cDesc;
INLabel.text = [NSString stringWithFormat:@"%i", myObj.ssID];
cell = automativeSearchCell;
}
/// other if statments for different types of cells
//Disclosure Indicator
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
any help would be greatly appreciated
It looks like you have a couple of problems. You shouldn’t be calling heightForRowAtIndexPath:, it’s called by the table view. You should set up any heights you need there based on the index path. It also looks like your dequeuing a normal UITableViewCell every time, and then switching to your other cell if a string is too long. You should do the string size check first, and then dequeue the proper cell. Also, it looks like you’re loading the nib every t1me you have the too long string. Instead, you should just register your nib (in viewDidLoad perhaps) and then use the dequeueReusableCellWithIdentifier:forIndexPath: method instead of the older method you’re using. That method is always guaranteed to dequeue a cell, so there’s no need for the if cell == nil clause.