简体   繁体   English

UITableView的单元格不能用作设置

[英]UITableView's cell doesn't work as setting

I set uitableview's cell as following 我将uitableview的单元格设置如下

 - (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, 我希望第一个单元格(indexPath.row = 0)与其他单元格不同,第一个单元格设置为bigphoto,其背景颜色为红色(使用其UI设计),其他为myphoto.png,黑色背景(使用UI)设计详情为DetailCell,但代码运行错误,

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... 第一个单元格(indexPath.row = 0)是正确的,但indexPath.row = 3或6或9 ..与indexPath.row = 0相同,不像indexPath.row = 1或2或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. 在您的情况下,最好的选择是使用静态tabelViewCells。 Please check on this link . 请检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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