繁体   English   中英

UITableView的单元格不能用作设置

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

我将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;
}

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

第一个单元格(indexPath.row = 0)是正确的,但indexPath.row = 3或6或9 ..与indexPath.row = 0相同,不像indexPath.row = 1或2或4 ...

那我该怎么办呢? 谢谢

这是因为第一个细胞被“重复使用”。 由于您将这些单元格用于两个单独的目的,因此在故事板中使用原型单元格仅用于第一行,然后以其他方式重用其余行的单元格将更加清晰。

正在重用该单元,您应该为具有不同表示的单元使用不同的单元标识符。 这应该工作:

- (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;
}

在您的情况下,最好的选择是使用静态tabelViewCells。 请检查此链接

暂无
暂无

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

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