【问题标题】:UITableViewCell position change while scroll UITableView滚动 UITableView 时 UITableViewCell 位置发生变化
【发布时间】:2013-11-05 18:39:50
【问题描述】:

滚动UITableView 时我的UITableViewCell 位置发生了变化。在这里我有更多UITableViewCell。所以它覆盖了不止一页的屏幕。当我从上到下滚动时,UITableViewCell 的位置会发生变化,例如第一个单元格排在第 5 个。第二个细胞来到第一个等等。

我的代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"SignUpWithEmailCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    switch (indexPath.row) {

        case 0: {

            UILabel *lblFirstName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblFirstName.text = @"First Name";
            [cell.contentView addSubview:lblFirstName];

            self.txtFirstName = [[UITextField alloc]initWithFrame:CGRectMake(lblFirstName.frame.origin.x + lblFirstName.frame.size.width + 5, 0, 190, 40)];
            self.txtFirstName.delegate = self;
            self.txtFirstName.text = strFirstName;
            self.txtFirstName.tag = 0;
            self.txtFirstName.userInteractionEnabled = YES;
            self.txtFirstName.font = [UIFont systemFontOfSize:15.0];
            self.txtFirstName.clearButtonMode = UITextFieldViewModeWhileEditing;
            self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            [cell.contentView addSubview:self.txtFirstName];
            break;
        } case 1: {

            UILabel *lblLastName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblLastName.text = @"Last Name";
            [cell.contentView addSubview:lblLastName];

            self.txtLastName = [[UITextField alloc]initWithFrame:CGRectMake(lblLastName.frame.origin.x + lblLastName.frame.size.width + 5, 0, 190, 40)];
            self.txtLastName.backgroundColor = [UIColor clearColor];
            self.txtLastName.delegate = self;
            self.txtLastName.tag = 1;
            self.txtLastName.text = strLastName;
            self.txtFirstName.userInteractionEnabled = YES;
            self.txtLastName.returnKeyType = UIReturnKeyDone;
            self.txtLastName.clearButtonMode = UITextFieldViewModeWhileEditing;
            self.txtLastName.font = [UIFont systemFontOfSize:15.0];
            self.txtLastName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            [cell.contentView addSubview:self.txtLastName];
            break;
        }
        default:
            break;
    }
}

 return cell;
}

谁能指导我正确存档..

提前致谢。

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    为了优化内存,iOS 初始化并仅使用屏幕显示的单元格。当您滚动查看下一个内容时,它会将不可见单元格出列并将其用于新的可见单元格。因此,第 1 个单元格用于第 8 行,依此类推。现在,在您的代码中,当此出列操作发生时,您不会重置内容。您的所有单元格内容代码仅在单元格不存在时才会出现。从您的 IF 条件中取出单元初始化代码,您应该一切顺利。

    【讨论】:

      【解决方案2】:

      即使单元格不为零,您也必须设置单元格的可视元素。如果您阅读文档,您会注意到 Apple 表示必须始终以这种方法更新单元格的内容。原因是检查 nil 只是测试您是否必须创建一个新单元格。如果您不需要创建新单元格,则意味着您正在重用一个单元格,因此它很可能会显示与您预期不同的行中的内容。因此,您应该在此方法中每次都重置内容。

      来自this document

      tableView:cellForRowAtIndexPath 的表格视图的数据源实现:应该总是在重用单元格时重置所有内容。

      这里有一些代码可以帮助您入门...但是由于您要在此方法中向单元格添加子视图,因此需要对其进行改进。只有当单元格为 nil 时才会发生这种情况,并且您应该存储对这些子视图的引用,以便在其他时间可以访问它们。

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
      (NSIndexPath *)indexPath {
      
          static NSString *CellIdentifier = @"SignUpWithEmailCell";
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      
          if (cell == nil)
          {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
              cell.backgroundColor = [UIColor clearColor];
              cell.selectionStyle = UITableViewCellSelectionStyleNone;
          }
      
          switch (indexPath.row) {
      
              case 0: {
      
                  UILabel *lblFirstName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
                  lblFirstName.text = @"First Name";
                  [cell.contentView addSubview:lblFirstName]; // TODO: MUST BE MOVED
      
                  self.txtFirstName = [[UITextField alloc]initWithFrame:CGRectMake(lblFirstName.frame.origin.x + lblFirstName.frame.size.width + 5, 0, 190, 40)];
                  self.txtFirstName.delegate = self;
                  self.txtFirstName.text = strFirstName;
                  self.txtFirstName.tag = 0;
                  self.txtFirstName.userInteractionEnabled = YES;
                  self.txtFirstName.font = [UIFont systemFontOfSize:15.0];
                  self.txtFirstName.clearButtonMode = UITextFieldViewModeWhileEditing;
                  self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
                  [cell.contentView addSubview:self.txtFirstName]; // TODO: MUST BE MOVED
                  break;
              } case 1: {
      
                  UILabel *lblLastName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
                  lblLastName.text = @"Last Name";
                  [cell.contentView addSubview:lblLastName]; // TODO: MUST BE MOVED
      
                  self.txtLastName = [[UITextField alloc]initWithFrame:CGRectMake(lblLastName.frame.origin.x + lblLastName.frame.size.width + 5, 0, 190, 40)];
                  self.txtLastName.backgroundColor = [UIColor clearColor];
                  self.txtLastName.delegate = self;
                  self.txtLastName.tag = 1;
                  self.txtLastName.text = strLastName;
                  self.txtFirstName.userInteractionEnabled = YES;
                  self.txtLastName.returnKeyType = UIReturnKeyDone;
                  self.txtLastName.clearButtonMode = UITextFieldViewModeWhileEditing;
                  self.txtLastName.font = [UIFont systemFontOfSize:15.0];
                  self.txtLastName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
                  [cell.contentView addSubview:self.txtLastName]; // TODO: MUST BE MOVED
                  break;
              }
              default:
                  break;
          }
      
      
       return cell;
      }
      

      【讨论】:

        【解决方案3】:

        这是你的问题if (cell == nil) 第一次单元格为零时,我创建了一个单元格,然后我尝试重用 de 单元格,单元格不为空,所以我像以前一样添加这个单元格,不做任何更改,因为解决您的问题,您必须删除 if 并且没有泄漏更好的是您制作一个包含所有对象的自定义单元格。

        【讨论】:

        • 我删除了 'if (cell == nil)' 循环。现在它工作正常。但我有一个疑问。 UITableViewCell 滚动时是否连续创建?它会显示一些内存警告吗??
        • 是的,每次你添加你用 addSubview 添加的所有项目时:在最后一个项目上,因为你最好创建一个自定义单元格并只替换对象的值
        • 如果你必须做 2 个不同的类型或单元格,你制作 2 个 customCell 然后你切换它们,如果是 case 0,你返回 firstCustomCell 否则你制作并返回 secondCustomCell
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多