滚动时Tableview单元格放错位置

时间:2014-07-13 13:23:05

标签: ios tableview

我正在创建一个tableview。在cellforrowatindexpath方法中,我创建了一个标签和一个imageview。一切都运作良好,直到这里。但滚动桌面视图时,单元格放错了位置。这是我使用的代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    NameLabel = [UILabel new];
    NameLabel.frame = CGRectMake(143, 8, 95, 25);
    NameLabel.font = [UIFont fontWithName:@"BBCNassim" size:16.0];
    //NameLabel.highlightedTextColor = [UIColor whiteColor];
    NameLabel.textAlignment = NSTextAlignmentRight;
    NameLabel.textColor = [UIColor darkGrayColor];
    [cell.contentView addSubview:NameLabel];

    IconLabel = [UIImageView new];
    IconLabel.frame = CGRectMake(245, 8, 24, 24);
    [cell.contentView addSubview:IconLabel];
}

if (indexPath.section == 0)
{
    [NameLabel setText:[self.menuItemsFirst objectAtIndex:indexPath.row]];
    [IconLabel setImage:[self.menuImgsFirst objectAtIndex:indexPath.row]];
}
if (indexPath.section == 1)
{
    [NameLabel setText:[self.menuItemsSecond objectAtIndex:indexPath.row]];
    [IconLabel setImage:[self.menuImgsSecond objectAtIndex:indexPath.row]];
}
if (indexPath.section == 2)
{
    [NameLabel setText:[self.menuItemsThird objectAtIndex:indexPath.row]];
    [IconLabel setImage:[self.menuImgsThird objectAtIndex:indexPath.row]];
}

return cell;
}

3 个答案:

答案 0 :(得分:1)

试试这个,

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil)
   {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

   }

      NameLabel = [UILabel new];
      NameLabel.frame = CGRectMake(143, 8, 95, 25);
      NameLabel.font = [UIFont fontWithName:@"BBCNassim" size:16.0];
      //NameLabel.highlightedTextColor = [UIColor whiteColor];
      NameLabel.textAlignment = NSTextAlignmentRight;
      NameLabel.textColor = [UIColor darkGrayColor];
      [cell.contentView addSubview:NameLabel];

      IconLabel = [UIImageView new];
      IconLabel.frame = CGRectMake(245, 8, 24, 24);
      [cell.contentView addSubview:IconLabel];

      if (indexPath.section == 0)
      {
        [NameLabel setText:[self.menuItemsFirst objectAtIndex:indexPath.row]];
        [IconLabel setImage:[self.menuImgsFirst objectAtIndex:indexPath.row]];
      }
      if (indexPath.section == 1)
      {
        [NameLabel setText:[self.menuItemsSecond objectAtIndex:indexPath.row]];
        [IconLabel setImage:[self.menuImgsSecond objectAtIndex:indexPath.row]];
      }
      if (indexPath.section == 2)
      {
        [NameLabel setText:[self.menuItemsThird objectAtIndex:indexPath.row]];
        [IconLabel setImage:[self.menuImgsThird objectAtIndex:indexPath.row]];
      }

 return cell;
}

答案 1 :(得分:1)

我觉得你的NameLabelIconLabel是全局变量。每个单元格的所有不同数据集都会覆盖相同的元素。

您应该在cellForRowAtIndexPath本地创建标签,并将它们添加到每个单元格中。

答案 2 :(得分:0)

在表视图中请求可重用单元格的正确方法是:

UITableViewCell *cell = [tableView 
        dequeueReusableCellWithIdentifier:CellIdentifier 
                             forIndexPath:indexPath];

...所以添加forIndexPath:indexPath