2011-10-04 7 views
0

こんにちは私は、ユーザーが選択されたときにサブビューから値を割り当てているカスタムuitableviewセルの単一のセクションがあります。どのようにuiableviewcellをクリアする

カスタムセルは2 textlabels、1のは、それはそれに割り当てられた「空」の文字列を持っているassingnedないがサブビューから選ばれた要素は、(割り当てされる場所他方はタイトルで構成されています。

私は単一のセルをスワイプして、ユーザーが値を割り当てている場合に、ユーザーがuilabelの内容を "空"にクリアできるようにしたいと考えています。

私は、私はuitableviewがユーザーが再びtableviewに触れるまで応答しなくなる問題を持っていますが、正常に動作します。

これは私の実装です。これが唯一の方法であるかどうか、またはスワイプを使用した後の表の応答性に問題があるため、これがより良い方法であるかどうかを知りたいと思います。

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return @"Clear"; 
} 

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     //Clears the correct uitableviewcell and releases the correct nsstring in the view 
     if (indexPath.section == 0) { 
      if (indexPath.row == 0) { 
       //reset uitableviewcell textlabel with default input "empty" 
      vehicleSearchCell = [tableView cellForRowAtIndexPath:indexPath]; 
      UILabel *label2; 
      label2 = (UILabel *)[vehicleSearchCell viewWithTag:2]; 
      label2.text = @"empty"; 
      [tableView reloadData]; //may or may not need to call this now 
      } 
      else if (indexPath.row == 1) { 
       //reset uitableviewcell textlabel with default input "empty" 
      } 
      else if (indexPath.row == 2) { 
       //reset uitableviewcell textlabel with default input "empty" 
      } 
      else (indexPath.row == 3) { 
       //reset uitableviewcell textlabel with default input "empty" 

      } 

     } 
    } 
} 

将来のアップデート。 私はボタン(カスタムセルにはボタンがあります)を使用している別のカスタムセルもありますが、ユーザーはこれをスワイプすることもできますが、クリアボタンもこれに表示されますが、クリア後にボタンが使用されています(明らかに線量はありません)。別のセルを初めて押すことはできますが、遅延はありません。しかし、セクション0のセルのいずれかを使用する場合は、反応しません。 enter image description here

更新 タグを使用してセルをロードしているように思っています。これは問題を引き起こしている可能性がありますか? manufactureSearchObjectStringがあなたのクラスのメンバ変数である場合、私はあなたが割り当てられていることを前提とどのように私はこの問題を解決することができます。..

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     [[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCell" owner:self options:nil]; 
     cell = vehicleSearchCell; 
     self.vehicleSearchCell = nil; 
    } 
    // Configure the cell... 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    if(indexPath.section == 0) 
    { 
     if(indexPath.row == 0) 
     { 
      UILabel *label1; 
      label1 = (UILabel *)[cell viewWithTag:1]; 
      label1.text = @"Manufacture"; 

      UILabel *label2; 
      label2 = (UILabel *)[cell viewWithTag:2]; 
      label2.text = manufactureSearchObjectString; 
     } 
     else if(indexPath.row == 1) 
     { 
      [[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCellDual" owner:self options:nil]; 
      cell = vehicleSearchCellDual; 
      self.vehicleSearchCellDual = nil; 

      UILabel *label; 
      label = (UILabel *)[cell viewWithTag:1]; 
      label.text = @"Model"; 

      UILabel *label2; 
      label2 = (UILabel *)[cell viewWithTag:2]; 
      label2.text = @"empty"; 

      UILabel *label3; 
      label3 = (UILabel *)[cell viewWithTag:3]; 
      label3.text = @"empty"; 
     } 
     else if(indexPath.row == 2) 
     { 
      UILabel *label; 
      label = (UILabel *)[cell viewWithTag:1]; 
      label.text = @"Year"; 

      UILabel *label2; 
      label2 = (UILabel *)[cell viewWithTag:2]; 
      label2.text = @"empty"; 
     } 
     else if(indexPath.row == 3) 
     { 
      UILabel *label; 
      label = (UILabel *)[cell viewWithTag:1]; 
      label.text = @"Key type"; 

      UILabel *label2; 
      label2 = (UILabel *)[cell viewWithTag:2]; 
      label2.text = keyTypeSearchObjectString; 
     } 
    } 
    else if(indexPath.section == 1) 
    { 
     if(indexPath.row == 0) 
     { 
      [[NSBundle mainBundle] loadNibNamed:@"VehicleSearchButtonCell" owner:self options:nil]; 
      cell = searchButtonCell; 
      self.searchButtonCell = nil; 
     } 
    } 
    return cell; 
} 

答えて

0

私は基本的にビューを削除しました.hmxibはファイルに直接私のコードをコピーし、完全に動作します...なぜこれが多分何か起こったのか分かりません私は非常に最初にそれを設定しました。私は確信していません...

2

私はあなたのコードに少し混乱し、何かに悩んだ

manufactureSearchObjectString = @"empty"; //Is manufactureSearchObjectString a member variable? 
    [self.tableView reloadData]; //reloads the tabels so you can see the value. 
    [manufactureSearchObjectString release]; //Why do you release this here?? This will leak/throw a wobbly more than likely. 

そうであればそれはある時点で。あなたはそれを他のどこかに指す前に最初に解放したいと思うでしょう。したがって、上記のコードは

[manufactureSearchObjectString release]; 
manufactureSearchObjectString = [[NSString alloc] initWithString:@"empty"]; 
[self.tableView reloadData]; //reloads the tabels so you can see the value. 

になります。それ以外の場所に割り当てていない場合は、リリースはまったく必要ありません。

はまた、あなたは、switchステートメントを使用して、あるいは「他の場合の」ではなく、プレーンのを使用してに見てみたいことがありますステートメントを変更

if (indexPath.row == 0) //if row is 0 test is correct 
    { 
    } 
    if (indexPath.row == 1) //if row is 0 and passed the first test this still gets tested here 
    { 
    } 
    if (indexPath.row == 2) //same again row gets tested again when it's already passed the == 0 test 

としてこれまで

if (indexPath.row == 0) //if row is 0 test is correct 
{ 
} 
else if (indexPath.row == 1) //if row is 0 it's not so will not get tested again 
{ 
} 
else if (indexPath.row == 2) 
{} 
else //remember to finish with a plain else 

番目のオプションれます==テストは、成功に達するまでのみ発生するため、ほとんどの場合、最初のテストよりも高速に実行します。これにかかる時間は、テーブルの行数とセクション数に依存します。

しかし、もっと一般的な方法を使用したい場合は、cellForRowAtIndexPath:を呼び出してそこに直接アクセスしてください。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     CustomUITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; 
     [cell setSecondCustomTextField:@"empty"]; //or could be cell.secondCustomTextField = @empty"; 
     [tableView reloadData]; //may or may not need to call this now 
    } 
} 
私はあなたがあなたのカスタムテーブルビューのセルに編集可能なテキストフィールドを呼んだもののセッターを持っていると思っています。そうでなければ、あなたは1つ作るべきです。

EDIT:あなたは

[[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCellDual" owner:self options:nil]; 

ではなく、何にでもそれを参照するに呼んでいる

さてさて、あなたはこれを行う必要はありません。私はそれをNIBからカスタムUITableViewCellを読み込もうとしていますが、これについて間違った方法をとっています。私はあなたがthisカスタムテーブルのビューのセルに関する愛のポストとココを読むことをお勧めします。
UITableViewCellに2つのテキストフィールドを追加すると、カスタムNIBがやや過剰ですが、そうすることを選択したように私はあなたの質問にそのように答えます。 最初は、それぞれが似ている場合、3つのカスタムセルを持つポイントはあまりありません。カスタムのUITableViewCell Nibファイルを作成するだけで簡単にコードを読み込むことができます。そのため、nibファイルを "VehicleSearchCellDual"に使用します。これは、大部分の機能を持つため、他のものは無視します2つの "VehicleSearchCell"と "VehicleSearchButtonCell"を必要としないので、 "VehicleSearchButtonCell"からUIButtonをコピーして "VehicleSearchCellDual"に挿入し、それをインターフェースビルダーで非表示に設定します。また、タグ値を4にします。インターフェースビルダーのタグを一致させるあなたが呼んでいる値は、viewWithTagを呼び出します:あなたが「VehicleSearchCellDual」Nibを使用するようになっているのを見ると、あなたは何をしているのでしょうか?タグ1は左端のテキストフィールドに、タグ2は中央のテキストフィールド、タグ3は右端のテキストフィールドです。 これで、テーブルセルを次のように読み込むことができました。

あなたがどんなメンバ変数にそれらを設定することをお勧めします@「空」にrightMostLabelとleftMostLabelを設定している場所の上今
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCellDual" owner:self options:nil]; 
    for(NSObject* nibItem in nib) 
    { 
     if([nibItem isKindOfClass:[UITableViewCell class]]) 
     { 
      cell = (UITableViewCell*)nibItem; 
      break; 
     } 
    } 
} 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
if(indexPath.section == 0) 
{ 
    UILabel* titleLabel; 
    titleLabel = (UILabel *)[cell viewWithTag:1]; 

    UILabel* rightMostLabel; 
    rightMostLabel = (UILabel *)[cell viewWithTag:3]; 

    if(indexPath.row == 0) 
    { 
     [titleLabel setText:@"Manufacture"]; 
     [rightMostLabel setText:manufactureSearchObjectString]; 
    } 
    else if(indexPath.row == 1) 
    { 
     [titleLabel setText:@"Model"]; 
     [rightMostLabel setText:@"empty"]; 
     UILabel* leftMostLabel; 
     leftMostLabel = (UILabel *)[cell viewWithTag:2]; 
     leftMostLabel.text = @"empty"; 
    } 
    else if(indexPath.row == 2) 
    { 
     [titleLabel setText:@"Year"]; 
     [rightMostLabel setText:@"empty"]; 
    } 
    else if(indexPath.row == 3) 
    { 
     [titleLabel setText:@"Key type"]; 
     [rightMostLabel setText:keyTypeSearchObjectString]; 
    } 
} 
else if(indexPath.section == 1) 
{ 
    if(indexPath.row == 0) 
    { 
     [[cell viewWithTag:1] setHidden:YES]; 
     [[cell viewWithTag:2] setHidden:YES]; 
     [[cell viewWithTag:3] setHidden:YES]; 
     [[cell viewWithTag:4] setHidden:NO]; 
    } 
} 
return cell; 

は「searchStringの」その場に適しています。それ以外の場合は、テーブルがリロードされるたびに、それらのフィールド値が「空」に設定されます。 これは正しい軌道上に設定する必要があります。

+0

代わりに[[manufactureSearchObjectString release]; manufactureSearchObjectString = [[NSString alloc] initWithString:@ "空"]; 'self.manufactureSearchObjectString = @" empty ";のような適切なプロパティー; – vikingosegundo

+0

私は上記のコードを更新しましたが、それでも動作しません.. –

+0

@ Jamie私はあなたのコードを上記の[manufactureSearchObjectString release] ; manufactureSearchObjectString = [[NSString alloc] initWithString:@ "empty"]; [self.tableView reloadData];私がスワイプした直後にテーブルはまだ応答しなくなっています。 –

関連する問題