Iphone – Trouble with tableview cell style – UITableViewCellStyleValue1

iphonetableview

I'm using a table view to display a list. Only one cell will have UITableViewCellStyleValue1. The problem is that when scrolling up/down, the detailed text is not displaying well. here's the code.

    // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
  if(indexPath.row == 0)
  {
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
   cell.textLabel.textColor = [UIColor whiteColor];
   cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
   cell.detailTextLabel.textColor = [UIColor yellowColor];
   cell.detailTextLabel.text = @"Description";
  }
  else
  {
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   cell.textLabel.textColor = [UIColor whiteColor];
   cell.accessoryType = UITableViewCellAccessoryNone;
  }
    }

 // Configure the cell.
 cell.textLabel.text = [radioList objectAtIndex:indexPath.row];
    return cell;
}

Can some one help me?

Best Answer

You are not handling the cell reuse correctly. Try doing it like this instead and I think you should be able to see what I'm doing differently.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
  UITableViewCell *cell = nil;
  if(indexPath.row == 0)
  {
   cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
   if (cell == nil)
   {
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier2] autorelease];
   }
   cell.textLabel.textColor = [UIColor whiteColor];
   cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
   cell.detailTextLabel.textColor = [UIColor yellowColor];
   cell.detailTextLabel.text = @"Description";
  }
  else
  {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.accessoryType = UITableViewCellAccessoryNone;
  }
 }

 // Configure the cell.
 cell.textLabel.text = [radioList objectAtIndex:indexPath.row];
 return cell;
}
Related Topic