2011-10-25 10 views
1

私はこの問題に1日ほどかかっています...私はこのフォーラムだけでなく、Googleや他のブログなどを検索しました。利用可能。UITable GUI要素を持つ複数のセクションを表示UIButton、UISwitchなど

、各行は異なるGUI要素(つまり、設定画面)

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

を持っていると私は達成するために探していますすべてがある何私が複数のセクションと、各セクション内UITableViewを作成してやっています各セクションにはさまざまなセルにさまざまなGUI要素が含まれています。しかし、あなたはこのスクリーンショットで見ることができるよう:

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

最初はすべてがそれが必要として表示されますが、一度上下にスクロールした後、問題が始まります。要素がシャッフルするようになり、要素の移動量が増えるほど上下にスクロールします。

![ "セクションと行のGUI要素とのUITableView。バグ?グリッチ?無知?"] [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; 
} 

ありがとうございます。

ケン


助けをALON感謝。

おそらく私はあなたが何を意味しているのか正確に理解していませんが、私はあなたの提案を適切に実装したと思います。しかし、私はまだ約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"を使用してセルを最初に取得する理由です。セルは、前にロードされたセルを指し示すnilではない可能性があります。あなたのケースでは、セルがゼロではない場合でも、サブビューを追加します。これは上下にスクロールすると無限に繰り返されます。ここで

が小さい例である:あなたの非常に特定の場合には

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

} 

、あなたはthis-

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

ok。私はおそらくあなたが何を意味しているかを正確に理解していないでしょ私はあなたの提案を実装したと思うが。不具合を取り戻すには約5以上のUPとDOWNが必要ですが、まだそこにあります。 – frederickk

+0

私の答えを変更しました。付属のサンプルを参照してください。 –

1

変更静的NSStringの* CellIdentifier = @ "セル" を書きます。各セクションのは次のようになります。

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

関連する問題