2012-02-23 15 views
1

私はURLのリストを格納する私のアプリにUITableViewを持っています。上の表にUITextFieldとUIButtonがあります。 ユーザがtextFieldにいくつかのURLを入力してボタンを押すと、そのURLをtableViewの一番上の要素のように追加します。 次に、ユーザーが任意のURLを選択すると、選択されたUITableViewCell内にボタンを作成して、そのURLに従うようにします(実際は重要ではありません)。ここでdidSelectRowAtIndexPathメソッドの定義です:UITableViewCellの設定

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [self.recentUrlsTableView cellForRowAtIndexPath:indexPath]; 

    UIButton *button = [UIButton new]; 
    button.frame = CGRectMake(cell.frame.size.width - 34, cell.frame.origin.y + 10, 24, 24); 
    button.backgroundColor = [UIColor lightGrayColor]; 
    [button setTitle:@">" forState:UIControlStateNormal]; 
    [cell.contentView addSubview:button]; 
    [button release]; 
} 

しかし、私は、セルの選択を解除したときに、それらのボタンが消えたいです。だから、私はこの事(私はそれが愚かに見えるKOWが、残念ながら、私はそこに私のボタンを見つけるためのより良い方法を知りませんでした)試してみました:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [self.recentUrlsTableView cellForRowAtIndexPath:indexPath]; 
    for (UIView *subview in cell.subviews) 
    { 
     if ([subview isKindOfClass:[UIButton class]]) 
      if ([[(UIButton*)subview titleLabel].text isEqualToString:@">"]) 
      { 
       [subview removeFromSuperview]; 
       [subview release]; 
      } 
    } 
} 

をしかし、このようなものは、正しく動作するように見えた - 代わりに解放すると消えますまったく、ボタンは別の行に開始されました。

その後、私は- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathメソッドのすべての行に対して同じアクションを実行しようとしました。しかし、それはまた助けにはなりませんでした。

私は、いくつかの些細な問題があると思いますが、私はそれを見つけることができません。だから、私はあなたの助けを非常に感謝します!

答えて

1

あなた/上下にスクロールするテーブルのセルを再利用取得し、(ボタンで)セルを完全diffetentコンテンツ(すなわち、異なるindexpathために使用される)を移入することができます。あなたがこれを体験している理由です。

テーブルには常に1つのボタン(またはなし)しかありません。

あなたが(それの&キープ参照reatin)テーブルの外側から1つのボタンを作成する場合は、オフに最高のだろう。これはあなたがreloadDataとcellForIndexPathの組み合わせを使用する必要があります動作しない場合

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     UITableViewCell *cell = [self.recentUrlsTableView cellForRowAtIndexPath:indexPath]; 

     //you have outside reference for this button 
     [button removeFromSuperView]; 
     button.frame = CGRectMake(cell.frame.size.width - 34, cell.frame.origin.y + 10, 24, 24); 
     [cell.contentView addSubview:button]; 
} 

:あなたのdidDeselectRowAtIndexPathは、次のようになります。 - クリックせず、右の表の適切な行を選択した後のリンクにアクセスするために

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
     { 
      [self.recentUrlsTableView reloadData];  
     } 

    - (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     { 
      //create your cell as usually 

      if ([cell isSelected]) { //i'm not sure if this is the right way to detect selected cell 

       [button removeFromSuperView]; 
       button.frame = CGRectMake(cell.frame.size.width - 34, cell.frame.origin.y + 10, 24, 24); 
       [cell.contentView addSubview:button]; 

      } 
     } 
+0

うーん、多分私は何か間違ったことをやったが、変異型のどちらも動作します...しかし、私は別の解決策を見つけた:そうのようにもう一つのボタン - 実際に私はそれがより論理的であることがわかります。 – Solomiya

+0

とにかく、ありがとうございました! – Solomiya

+0

実際、あなたの最終的な解決策はおそらく最高のものです:) –

関連する問題