2013-03-27 20 views
15

XIBファイルTimerCell.xibUITableViewCellがあります。私は2つのUILabelと1 UIButtonを持っている私のTimerCellUITableViewCellでUIButtonのアクションを設定する方法

static NSString *CellIdentifier = @"cellTimer"; 
    TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     //cell = [[TimerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 

cellForRowAtIndexPath内の他のクラスでは、私はこのUITableViewCellを初期化します。このボタンについては、アクションをいくつかの方法に設定したいと思います。

どうすればいいですか?そして、最初のUILabelのバックグラウンドカウントダウンタイマーのデータをリアルタイムに表示するにはどうすればいいですか?

答えて

25

コードのこの作品はあなたに

static NSString *CellIdentifier = @"cellTimer"; 
TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    //cell = [[TimerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.tag=indexPath.row; 
    [button addTarget:self 
     action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown]; 
    [button setTitle:@"cellButton" forState:UIControlStateNormal]; 
    button.frame = CGRectMake(80.0, 0.0, 160.0, 40.0); 
    [cell.contentView addSubview:button]; 
    } 

    return cell; 
} 


-(void)aMethod:(UIButton*)sender 
{ 
NSLog(@"I Clicked a button %d",sender.tag); 
} 

を助けるには、この情報がお役に立てば幸いです!

+0

それは働きます!ありがとうございました!!これでタグを設定する方法は?それ以前は** cell.staticButton.tag = indexPath.row; ** – Romowski

+0

私の編集した回答をチェック@Romowski – Dany

-3

SOに投稿する前に、より良い調査をしてください。ここ あなたがTimerCellという名前UITableViewCellのサブクラスを持っているので、あなたがTimerCellにアウトレットプロパティを追加することができます

UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[Button addTarget:self action:@selector(MethodName:) forControlEvents:UIControlEventTouchUpInside]; 
[Button setTitle:@"ButtonTitle" forState:UIControlStateNormal]; 
Button.frame = CGRectMake(100.0, 10.0, 40.0, 40.0); 
[cell.contentView addSubview:Button]; 
+0

なぜそれがダウンリストされましたか?説明してください、または役に立たないです。違いますか?答えるか、質問に答えようとしていますか? – YSC

17

をセルにボタンを追加するコードです。例:

TimerCell.xibには、ボタンとラベルにアウトレットを接続します。

その後、tableView:cellForRowAtIndexPath:に、あなたは簡単にそのターゲットとアクションを設定するためのボタンにアクセスすることができます:

static NSString *CellIdentifier = @"cellTimer"; 
TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 

[cell.button addTarget:self action:@selector(cellButtonWasTapped:) 
    forControlEvents:UIControlEventTouchUpInside];; 

あなたは時にタイマー火災、それを更新するために、そのlabelプロパティを使用して、セルのラベルにアクセスすることができます。

this answerで説明したように、ボタンとラベルを表示するもう1つの方法は、ビュータグを使用することです。

cellButtonWasTapped:(またはボタンから送信するアクション)で、ボタンを含むセルのインデックスパスをルックアップしたいと思うかもしれません。私はthis answerでそれを行う方法を説明します。

+0

UITableViewCellで現在のタイマーの値を表示する方法を理解できません...このことをもう一度説明できますか?詳細については、タイマーはシングルトンで動作します。タイマーは1つしかありませんが、時間値を含む複数の項目を持つ辞書があります。毎分タイマーは、辞書内のすべての項目から1を減じます。 TimerCellは、ポップアップで表示されるtableViewのセルです。だから私のセルの中に辞書の項目の時間値を表示したい。ラベル...ありがとう – Romowski

+0

http://stackoverflow.com/questions/15653729/how-to-show-nstimers-time-value-in-uitableviewcells-label – Romowski

+0

+1大きな説明とドキュメント...あなたと一緒に行く10以上119,000;) – logixologist

3
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"cellTimer"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 
    NSInteger index = indexPath.row; 
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:101]; 
    UIButton *button = (UIButton *)[cell viewWithTag:100]; 
    [button addTarget:self action:@selector(MethodName:) forControlEvents:UIControlEventTouchUpInside]; 
} 

そして、あなたのXIBファイルでUIButtonとUILabelのタグを設定TimerCell.xib

関連する問題