2011-10-29 8 views
4

UITableViewCellのコンテンツビューにボタンを追加しようとして問題が発生しています。 ボタンが見えません。しかし、セルをクリックすると、(デフォルトの青色のバックグラウンドを使用して)セルが選択されると、ボタンが表示されます。UITableViewCellのコンテンツビューにUIButtonが追加されたのは、セルが選択されたときにのみ表示されます。

テーブルビューコントローラは、initWithStyle:UITableViewStyleGroupedで作成されます。

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


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

    theButton = [[UIButton alloc] initWithFrame:CGRectMake(800, 25, 32, 32)]; 
    [cell.contentView addSubview:theButton]; 
    theButton.tag = 1; 
    [theButton release]; 

} 

if(indexPath.section ==0){ 
     //...logic... 
    }else if(indexPath.section ==1){ 
     //...logic... 
    }else{ 
     cell.textLabel.text = @"some string"; 

    theButton = (UIButton*)[cell.contentView viewWithTag:1]; 
    [theButton setImage:theImage forState:UIControlStateNormal]; 
} 

return cell; 
} 

答えて

4

私は[UIColor clearColor]との明確なテーブルビューセルのテキストラベルの背景色を作ることによって、問題を解決しました。

+1

本当に助かりました!ありがとう! – HiveHicks

+0

うれしかったです! – tommi

関連する問題