滚动UITableView时更改UITableViewCell位置

时间:2013-10-26 18:23:06

标签: ios objective-c uitableview

滚动UITableViewCell时我的UITableView位置发生了变化。在这里,我有更多UITableViewCell。所以它涵盖了多个页面的屏幕。当我从上到下滚动时,UITableViewCell的位置发生了变化,就像第一个单元格变为第五个一样。第二个细胞来到第一个等等。

我的代码:

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

有人可以指导我正确存档..

先谢谢。

3 个答案:

答案 0 :(得分:1)

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

来自this document

  

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

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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