2011-10-25 58 views
1

我一直在這個問題上我的腦子一天左右現在...我已經搜索這個論壇,以及谷歌和其他博客等所有沒有無濟於事。UITableView與GUI元素UIButton,UISwitch等多個部分

我在做什麼是創建具有多個部分,並且每個部分內UITableView,每一行都有不同的GUI元素(即設置屏幕)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 3; 
} 

和所有我期待實現的每個部分在各個單元中具有各種GUI元素。但你可以在這個截圖中看到:

http://twitpic.com/75qix5/full

起初一切都顯得理所應當,但向上和向下滾動一次之後,問題開始。元素開始隨機拖動,並且越往上或向下滾動,元素移動得越多。

![ 「的UITableView在部分GUI元素和行。漏洞?毛刺?無知?」] [2]

這是一個錯誤?毛刺?或無知? (可能是後者) - 我已經除了GUI代碼以外的所有其他代碼,並且如果它意味着我正在使用NIB創建UITableView的任何東西

另外,這裏是我的cellForRowAtIndexPath方法的代碼。如果有人能指引我正確的方向或告訴我我做錯了什麼,我將永遠感激。

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 


    // Set up the cell 
    // desired section 
    if(indexPath.section == 0) { 
     // 
     // Load Settings 
     // 

     // button 
     UIButton * loadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     loadButton.frame = CGRectMake(0.0, 0.0, 300.0, 44.0); 
     [loadButton setTitle:@"Load Image" forState:UIControlStateNormal]; 
     [loadButton setTitle:@"Load Image" forState:UIControlStateSelected]; 
     [loadButton addTarget:self action:@selector(loadImage:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubview:loadButton]; 


    } else if(indexPath.section == 1) { 
     // 
     // Mode Settings 
     // 
     if(indexPath.row == 0) { 
      cell.text = [mode objectAtIndex:indexPath.row];  
     } else if(indexPath.row == 1) { 
      cell.text = [mode objectAtIndex:indexPath.row];  
     } 


    } else if(indexPath.section == 2) { 
     // 
     // Marker Settings 
     // 

     if(indexPath.row == 0) { 
      // description text 
      cell.text = [marker objectAtIndex:indexPath.row];  

      // switch 
      UISwitch *markerSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
      cell.accessoryView = markerSwitch; 
      [markerSwitch setOn:YES animated:NO]; 
      [markerSwitch addTarget:self action:@selector(markerToggle:) forControlEvents:UIControlEventValueChanged]; 
      [markerSwitch release];  

     } else if(indexPath.row == 1) { 
      UISlider *markerGridSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)]; 
      cell.accessoryView = markerGridSlider; 
      markerGridSlider.minimumValueImage = [UIImage imageNamed:@"size_icon_1x1.png"]; 
      markerGridSlider.maximumValueImage = [UIImage imageNamed:@"size_icon_8x8.png"]; 

      [markerGridSlider setMinimumValue:1.0]; 
      [markerGridSlider setMaximumValue:8.0]; 
      markerGridSlider.value = 8.0; 
      [markerGridSlider addTarget:self action:@selector(markerGrid:) forControlEvents:UIControlEventValueChanged]; 
      [markerGridSlider release];  

     } else if(indexPath.row == 2) { 
      UISlider *markerSpacingSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)]; 
      cell.accessoryView = markerSpacingSlider; 
      markerSpacingSlider.minimumValueImage = [UIImage imageNamed:@"spacing_icon_min.png"]; 
      markerSpacingSlider.maximumValueImage = [UIImage imageNamed:@"spacing_icon_max.png"]; 

      [markerSpacingSlider setMinimumValue:1.0]; 
      [markerSpacingSlider setMaximumValue:(320.0/8.0)]; 
      markerSpacingSlider.value = 1.0; 
      [markerSpacingSlider addTarget:self action:@selector(markerSpacing:) forControlEvents:UIControlEventValueChanged]; 
      [markerSpacingSlider release];  
     } 
    } 

    return cell; 
} 

謝謝。


感謝阿龍的幫助。或許我並不完全瞭解你的意思,但我認爲我已經正確實施了你的建議。不過,我仍然得到了交換位置的元素的小故障,雖然現在約5+的UP和DOWN擦除之後,我的UISwitch現在被放置在左上角。

這裏是我更新的代碼:

- (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] autorelease]; 



     if([indexPath section] == 0) { 
      // 
      // Load Settings 
      // 

      // button 
      UIButton *loadButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] autorelease]; 
      loadButton.frame = CGRectMake(0.0, 0.0, 300.0, 44.0); 
      [loadButton addTarget:self action:@selector(loadImage:) forControlEvents:UIControlEventTouchUpInside]; 
      [loadButton setTag:1000]; 

      [cell.contentView addSubview:loadButton]; 

     } 



     if([indexPath section] == 1) { 
      // 
      // Mode Settings 
      // 
      if([indexPath row] == 0) { 
       cell.text = [mode objectAtIndex:[indexPath row]];  
      } 
      if([indexPath row] == 1) { 
       cell.text = [mode objectAtIndex:[indexPath row]];  
      } 
     } 



     if([indexPath section] == 2) { 
      // 
      // Marker Settings 
      // 
      if([indexPath row] == 0) { 
       // switch 
       UISwitch *markerSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
       [markerSwitch setOn:YES animated:NO]; 
       [markerSwitch addTarget:self action:@selector(markerToggle:) forControlEvents:UIControlEventValueChanged]; 
       [markerSwitch setTag:3000]; 

       [cell.contentView addSubview:markerSwitch]; 
       [markerSwitch release]; 

      } 
      if([indexPath row] == 1) { 
       // slider 
       UISlider *markerGridSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)]; 
       markerGridSlider.minimumValueImage = [UIImage imageNamed:@"size_icon_1x1.png"]; 
       markerGridSlider.maximumValueImage = [UIImage imageNamed:@"size_icon_8x8.png"]; 

       [markerGridSlider setMinimumValue:1.0]; 
       [markerGridSlider setMaximumValue:8.0]; 
       [markerGridSlider setValue:8.0]; 
       [markerGridSlider addTarget:self action:@selector(markerGrid:) forControlEvents:UIControlEventValueChanged]; 
       [markerGridSlider setTag:3100]; 

       [cell.contentView addSubview:markerGridSlider]; 
       [markerGridSlider release]; 

      } 
      if([indexPath row] == 2) { 
       // slider 
       UISlider *markerSpacingSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)]; 
       markerSpacingSlider.minimumValueImage = [UIImage imageNamed:@"spacing_icon_min.png"]; 
       markerSpacingSlider.maximumValueImage = [UIImage imageNamed:@"spacing_icon_max.png"]; 

       [markerSpacingSlider setMinimumValue:1.0]; 
       [markerSpacingSlider setMaximumValue:(320.0/8.0)]; 
       [markerSpacingSlider setValue:1.0]; 
       [markerSpacingSlider addTarget:self action:@selector(markerSpacing:) forControlEvents:UIControlEventValueChanged]; 
       [markerSpacingSlider setTag:3200]; 

       [cell.contentView addSubview:markerSpacingSlider]; 
       [markerSpacingSlider release]; 

      } 
     } 


    } // end cell == nil 


    // Load 
    UIButton *loadButton = (UIButton*)[cell.contentView viewWithTag:1000]; 
    [loadButton setTitle:@"Load Image" forState:UIControlStateNormal]; 
    [loadButton setTitle:@"Load Image" forState:UIControlStateSelected]; 

    // Mode 

    // Marker 
    UISwitch *markerSwitch = (UISwitch*)[cell.contentView viewWithTag:3000]; 
    UISlider *markerGridSlider = (UISlider*)[cell.contentView viewWithTag:3100]; 
    UISlider *markerSpacingSlider = (UISlider*)[cell.contentView viewWithTag:3200]; 


    return cell; 
} 

回答

1

所有iOS中綁定到UITable細胞是可重複使用的。 這就是爲什麼你首先讓你的單元格調用「dequeueReusableCellWithIdentifier」函數,單元格可能不是零,指向之前加載的單元格。在你的情況下,即使單元格不是零,你也可以爲它添加子視圖。當你上下滾動時,這會發生無數次。

這裏是一個小例子:

-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 2; 
} 

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString* CellIdentifier = @"Cell"; 

    const NSInteger lblTag = 101; 
    const NSInteger btnTag = 102; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    //First init the cells 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     //Initialize subviews of cells only in this block. 

     if (indexPath.row == 0) { 
      //Only the first cell on the top has a UILabel 
      UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 20)] autorelease]; 
      lbl.tag = lblTag;  
      [cell.contentView addSubview:lbl]; 
     } else if (indexPath.row == 1) { 
      //Only the 2nd cell has a UIButton 
      UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
      btn.frame = CGRectMake(5, 5, 60, 20); 
      btn.tag = btnTag;  
      [cell.contentView addSubview:btn]; 
     } 
    } 

    //Modify your cells here 
    if (indexPath.row == 0) { 
     //Only the first cell on the top has a UILabel 
     UILabel *lbl = (UILabel*)[cell.contentView viewWithTag:lblTag]; 
     //Do stuff with our label 

     lbl.text = [NSString stringWithFormat:@"%d",indexPath.row]; 

    } else if (indexPath.row == 1) { 
     //Only the 2nd cell has a UIButton 
     UIButton *btn = (UIButton*)[cell.contentView viewWithTag:btnTag]; 
     //Do stuff with our button 
     [btn setTitle:[NSString stringWithFormat:@"%d",indexPath.row] forState:UIControlStateNormal]; 

    } 

    return cell; 

} 

在你非常特定的情況下,你會寫這個 -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (section == 0) { 
     return 1; 
    } else if (section == 1) { 
     return 2; 
    } else if (section == 2) { 
     return 3; 
    } else { 
     return 0; 
    } 
} 

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

    static NSString *CellIdentifier = @"Cell"; 



    const NSInteger 
    sec0BtnTag = 101, 
    markerSwitchTag = 102, 
    markerGridSliderTag = 103, 
    markerSpacingSliderTag = 104; 

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



     if([indexPath section] == 0) { 
      // 
      // Load Settings 
      // 

      // button 
      UIButton *loadButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] autorelease]; 
      loadButton.frame = CGRectMake(0.0, 0.0, 300.0, 44.0); 
      [loadButton addTarget:self action:@selector(loadImage:) forControlEvents:UIControlEventTouchUpInside]; 
      [loadButton setTag:sec0BtnTag]; 

      [cell.contentView addSubview:loadButton]; 

     } 



     else if([indexPath section] == 2) { 
      // 
      // Marker Settings 
      // 
      if([indexPath row] == 0) { 
       // switch 
       UISwitch *markerSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
       [markerSwitch setOn:YES animated:NO]; 
       [markerSwitch addTarget:self action:@selector(markerToggle:) forControlEvents:UIControlEventValueChanged]; 
       [markerSwitch setTag:markerSwitchTag]; 

       [cell.contentView addSubview:markerSwitch]; 
       [markerSwitch release]; 

      } 
      else if([indexPath row] == 1) { 
       // slider 
       UISlider *markerGridSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)]; 
       markerGridSlider.minimumValueImage = [UIImage imageNamed:@"size_icon_1x1.png"]; 
       markerGridSlider.maximumValueImage = [UIImage imageNamed:@"size_icon_8x8.png"]; 

       [markerGridSlider setMinimumValue:1.0]; 
       [markerGridSlider setMaximumValue:8.0]; 
       [markerGridSlider setValue:8.0]; 
       [markerGridSlider addTarget:self action:@selector(markerGrid:) forControlEvents:UIControlEventValueChanged]; 
       [markerGridSlider setTag:markerGridSliderTag]; 

       [cell.contentView addSubview:markerGridSlider]; 
       [markerGridSlider release]; 

      } 
      else if([indexPath row] == 2) { 
       // slider 
       UISlider *markerSpacingSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)]; 
       markerSpacingSlider.minimumValueImage = [UIImage imageNamed:@"spacing_icon_min.png"]; 
       markerSpacingSlider.maximumValueImage = [UIImage imageNamed:@"spacing_icon_max.png"]; 

       [markerSpacingSlider setMinimumValue:1.0]; 
       [markerSpacingSlider setMaximumValue:(320.0/8.0)]; 
       [markerSpacingSlider setValue:1.0]; 
       [markerSpacingSlider addTarget:self action:@selector(markerSpacing:) forControlEvents:UIControlEventValueChanged]; 
       [markerSpacingSlider setTag:markerSpacingSliderTag]; 

       [cell.contentView addSubview:markerSpacingSlider]; 
       [markerSpacingSlider release]; 

      } 
     } 


    } // end cell == nil 

    //Load specific cell row values 

    if([indexPath section] == 0) { 
     // 
     // Load Settings 
     // 

     // button 
     UIButton *loadButton = (UIButton*)[cell.contentView viewWithTag:sec0BtnTag]; 
     [loadButton setTitle:@"Load Image" forState:UIControlStateNormal]; 
     [loadButton setTitle:@"Load Image" forState:UIControlStateSelected]; 
     //do other stuff with the button here according to the specific row & section 

    } 



    if([indexPath section] == 1) { 
     // 
     // Mode Settings 
     // 
     if([indexPath row] == 0) { 
      cell.text = [mode objectAtIndex:[indexPath row]];  
     } 
     if([indexPath row] == 1) { 
      cell.text = [mode objectAtIndex:[indexPath row]];  
     } 
    } 



    if([indexPath section] == 2) { 
     // 
     // Marker Settings 
     // 
     if([indexPath row] == 0) { 
      // switch 
      UISwitch *markerSwitch = (UISwitch*)[cell.contentView viewWithTag:markerSwitchTag]; 
      //do stuff with the switch here according to the specific row & section 
     } 
     if([indexPath row] == 1) { 
      // slider 
      UISlider *markerGridSlider = (UISlider*)[cell.contentView viewWithTag:markerGridSliderTag]; 
      //do stuff with the slider here according to the specific row & section 
     } 
     if([indexPath row] == 2) { 
      // slider 
      UISlider *markerSpacingSlider = (UISlider*)[cell.contentView viewWithTag:markerSpacingSliderTag]; 
      //do stuff with the slider here according to the specific row & section 



     } 
    } 


    return cell; 
} 
+0

好的。我可能不完全瞭解你的意思。儘管我認爲我已經實施了你的建議。它需要大約5+ UP和DOWN來恢復故障,但它仍然存在。 – frederickk

+0

修改我的答案。查看包含的示例。 –

1

變化靜態的NSString * CellIdentifier = @ 「細胞」;像這樣:

NSString * CellIdentifier; if(indexPath.section == 0){CellIdentifier = @「Cell」; } elseif(indexPath.section == 2){CellIdentifier = @「Cell2」; }