简体   繁体   中英

UITableView's cell doesn't work as setting

I set uitableview's cell as following

 - (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   static NSString *CellIdentifier = @"Cell";
   DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
     cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }

if (indexPath.row == 0) {
    cell.backgroundColor = [UIColor redColor];
    UIImageView *bigphoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
    bigphoto.image = [UIImage imageNamed:@"bigphoto.png"];
    [cell addSubview:bigphoto];
}
else {
    cell.backgroundColor = [UIColor blackColor];
    cell.myphoto.image = [UIImage imageNamed:@"myphoto.png"];
    cell.phototime.text = @"2014-03-01";
}

 return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (indexPath.row == 0) {
      return 320;
  }
  return 220;
}

I want the first cell(indexPath.row = 0) is different from others, the first cell is set a bigphoto and its background color is red(with its UI Design), others are myphoto.png with black color background(with the UI Desigin of DetailCell), but the code runs with the wrong result,

the first cell(indexPath.row = 0) is right, but the indexPath.row = 3 or 6 or 9.. are the same as indexPath.row = 0, not like indexPath.row = 1 or 2 or 4...

so how can I fixed this? thanks

This is because the first cell is "reused." Since you are using these cells for two separate purposes, it would be much cleaner to have a prototype cell in the storyboard that you use for just the first row, and then otherwise reuse cells for the rest of the rows.

The cell is being reused, you should use different cell identifiers for cells with different representation. This should work:

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *CellIdentifier;
  DetailCell *cell = nil;
  if (indexPath.row == 0) {
    cellIdentifier = @"Cell0";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
      cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      cell.backgroundColor = [UIColor redColor];
      UIImageView *bigphoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
      bighoto.tag = 1;
      [cell addSubview:bigphoto];
    }
    UIImageView *bigphoto = (UIImageView *)[cell viewWithTag:1];
    bigphoto.image = [UIImage imageNamed:@"bigphoto.png"];
  }
  else {
    cellIdentifier = @"Cell1";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
      cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      cell.backgroundColor = [UIColor blackColor];
    }
    cell.myphoto.image = [UIImage imageNamed:@"myphoto.png"];
    cell.phototime.text = @"2014-03-01";
  }
  return cell;
}

In your case best option is to use the static tabelViewCells. Please check on this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM