2011-01-14 12 views
0

私はテーブルビューを持っています。各セル(行)に、私は2つのボタンを表示したい。最初は両方とも赤色である。 1つのボタンをクリックすると緑色に変わり、他のボタンは赤色に変わります。 私は2つのボタンを持つビューを作成しました。私はIBを使用してビューを作成しています。 私は、カスタムビューを表示するには、私のテーブルビューを表示するには、次のコードを使用しています:上記のコードiPhone - UITableViewCellのUIViewに2 UIButtonがある

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

NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:@"CellView" 
      owner:self options:nil]; 
CellView *myView; 
    for (id object in bundle) { 
     if ([object isKindOfClass:[CellView class]]) 
      myView = (CellView *)object; 
    } 
NSString* str = [testArray objectAtIndex:indexPath.row]; 
NSArray* arr = [str componentsSeparatedByString:@" "]; 
myView.left.titleLabel.text = [arr objectAtIndex:0]; 
myView.right.titleLabel.text = [arr objectAtIndex:1]; 

[cell.contentView addSubview:myView]; 
    return cell; 
} 

が正常に動作しますが、ボタンがクリックされたとき、その私がIBで作成したテキストとボタンを示しました。どうしてそれが起こっているのか分かりません。

セルにボタンを表示してその操作を処理する方法を教えてもらえますか?

答えて

1

ボタンの状態に応じてtitleLabelを指定する必要があると思います。その後、このコードは正常に動作します。

+0

そうです。単純なことを特定するのは間違いです。 – Satyam