2012-03-23 56 views
0

当我上下滚动tableview时,单元格中的内容会重叠,为什么是这样以及如何纠正它?当TableView滚动时,单元格内容重叠

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS 

IndexPath *)indexPath 
    { 

     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 

      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ; 

     } 

      UITextField* tf = nil ; 

       switch (indexPath.row) { 

        case 0: { 

         cell.textLabel.text = @"First Name" ; 

         tf = firstNameTextField = [self makeTextField:self. placeholder:@"F name"]; 

         [cell.contentView addSubview:firstNameTextField]; 

         break ; 

        } 

        case 1: { 

         cell.textLabel.text = @"Last Name" ; 

         tf = lastNameTextField = [self makeTextField:self.lastName placeholder:@"L name"]; 

         [cell.contentView addSubview:lastNameTextField]; 

         break ; 

        } 

        case 2: { 

         cell.textLabel.text = @"age" ; 

         tf = agetextfield = [self makeTextField:self.password placeholder:@"Age"]; 

         [cell.contentView addSubview:agetextfield]; 


         break ; 

        } 


       } 
       return cell; 

回答

1

细胞在这种情况下被重新使用。所以细胞在其他细胞上重叠。只需修改您的代码即可解决此问题。

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ; 
     UITextField* tf = nil ; 
     for(UIView *view in [cell subViews]){ 

      if([view isKindOfClass:[UITextFiel class]]){ 

        [view removeFromSuperView]; 
       } 
     } 
      switch (indexPath.row) { 

       case 0: { 

        cell.textLabel.text = @"First Name" ; 

        tf = firstNameTextField = [self makeTextField:self. placeholder:@"F name"]; 

        [cell.contentView addSubview:firstNameTextField]; 

        break ; 

       } 

       case 1: { 

        cell.textLabel.text = @"Last Name" ; 

        tf = lastNameTextField = [self makeTextField:self.lastName placeholder:@"L name"]; 

        [cell.contentView addSubview:lastNameTextField]; 

        break ; 

       } 

       case 2: { 

        cell.textLabel.text = @"age" ; 

        tf = agetextfield = [self makeTextField:self.password placeholder:@"Age"]; 

        [cell.contentView addSubview:agetextfield]; 


        break ; 

       } 


      } 
    } 


      return cell; 
+0

没有变化,我仍然得到相同的结果。 – Illep 2012-03-23 16:33:01

+0

您是否尝试过cell.contentView = lastNameTextField – rakeshNS 2012-03-23 16:38:36

+0

并将样式更改为UITableViewCellStyleDefault – rakeshNS 2012-03-23 16:39:59

0

你的代码很好,除了你永远不会从单元格的contentview中删除'旧'子视图。所以UITextField *tF = nil;之前添加以下代码:

for(UIView *v in cell.contentView.subviews){ 
    [v removeFromSuperview]; 
} 
+0

就在“ UITextField * tF = nil;“ – datwelk 2012-03-23 16:59:10

+0

当我完成上述操作并向下滚动时,第一个文本字段的值显示在最后一行 – Illep 2012-03-23 17:02:49

1

要添加UITextField到细胞每次细胞被重用。 这样做是为了增加它创建细胞,只有当,然后从现有单元

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    UITextField* tf = ...; 
    tf.tag = kMyTextFieldTag; 
    [cell.contentView addSubview:tf]; 
} 
UITextField* tf = (UITextField*)[cell.contentView viewWithTag:kMyTextFieldTag]; 
tf.text = ...; 

更妙的想法是让细胞的UITextField一个accessoryView,而不是将其添加为一个子视图让现有视图。 如果它不产生所需的结果,请考虑制作自定义单元格,而不是将视图添加到UITableViewCellStyleValue1单元格。

0

您需要将所有子视图(标签等)添加到您的cell.contentview只有一次,意​​味着可以modifiy你的cellForRowAtIndexPath方法如下:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ; 

UITextField* tf = nil ; 

      switch (indexPath.row) { 

       case 0: { 

        cell.textLabel.text = @"First Name" ; 

        tf = firstNameTextField = [self makeTextField:self. placeholder:@"F name"]; 

        [cell.contentView addSubview:firstNameTextField]; 

        break ; 

       } 

       case 1: { 

        cell.textLabel.text = @"Last Name" ; 

        tf = lastNameTextField = [self makeTextField:self.lastName placeholder:@"L name"]; 

        [cell.contentView addSubview:lastNameTextField]; 

        break ; 

       } 

       case 2: { 

        cell.textLabel.text = @"age" ; 

        tf = agetextfield = [self makeTextField:self.password placeholder:@"Age"]; 

        [cell.contentView addSubview:agetextfield]; 


        break ; 

       } 

    } 





      return cell; 
} 

你不需要从添加和删除您的子视图每次单元被重新加载时,单元都会被重新加载

+0

不要再次使用该单元格。 – vishiphone 2012-05-08 11:26:39

相关问题