2012-03-23 133 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

相關問題