2012-01-30 9 views
1

私はUIのtableViewを持っていると私は履歴のクリアにSafariの設定で使用されているようなボタンを作成しようとしています:は、テーブルビューのセルにボタンを追加します。

enter image description here

私が試してみましたコードをdoesntのは、本当に仕事と私はより良い方法があると確信している:

 case(2): 
      // cell = [[[ButtonLikeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 


      if (indexPath.row == 0) 
      {    
       UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
       [button setFrame:cell.bounds]; 
       [button addTarget:self action:@selector(clearButtonClick) forControlEvents:UIControlEventTouchUpInside]; 
       [button setBackgroundColor:[UIColor purpleColor]]; 
       [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
       [button setTitle:@"Clear Data" forState:UIControlStateNormal]; 
       cell.accessoryView = nil; 
       cell.clipsToBounds=YES; 
       [cell.contentView addSubview:button]; 
      } 

      break; 
    } 
} 

return(cell); 

}

答えて

4

テーブルビューセルのサブクラスは、このためにやり過ぎで、基本的にすべてを行う必要がある

case(2): 
     if (indexPath.row == 0) 
     { 
      cell.textLabel.textAlignment = UITextAlignmentCenter; 
     } 
     break; 
+0

あなたは正しいです! textLabelがsizeToFitを通過するような印象にあったので、位置合わせを変更しても機能しません。 –

2

実際UIButtonを使用する必要はありません。

行と「通常の」行の唯一の違いは、ラベルの位置です。 サブクラスのUITableViewCellなどのように、ラベルを中央に-laysubviewsをオーバーライドして、設定が完了している:

@interface ButtonLikeCell : UITableViewCell 

@end 

@implementation ButtonLikeCell 

-(void)layoutSubviews 
{ 
    self.titleLabel.center = self.center; 
} 

@end 

これは、セルのラベルを再配置し、それはあなたが探しているものに似て見えるようになります。

+0

あなたは少しHeziを拡張でしを中央にtextLabelのテキストの配置を設定されていますか?これはIOsの最初の数日です – JonWells

+0

は、uitableviewcellの中のボタンではなく、textLabelのtextAlignmentプロパティがUITextAlignmentCenterに設定されたuitableviewcellです。 – wattson12

+0

@CrimsonChin私は自分の答えを更新しました。 –

関連する問題