2012-08-04 60 views
1

我有我在我的CustomCell創建一個按鈕。如何在UITableViewCell中使用按鈕進行操作?

- (IBAction)onoffBtn:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 
    button.selected = !button.selected; 
    if (button.selected) 
    { 
     // Do action 1 
    } 
    else 
    { 
     // Do action 2 
    } 
} 

當我運行它時,按鈕顯示在我創建TableViewController UITableView的單元格中。我可以在我的牢房內按下onoffBtn,但它不能採取行動。我該如何做,以便我的按鈕可以執行操作?

這是我的我的TableViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]]; 
    if (cell == nil) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     cell = _customCell; 
     _customCell = nil; 
    } 
    // Show my cell 
    return cell; 
} 

和其它問題內的cellForRowAtIndexPath,在那裏我應該寫在做的過程嗎?在我的TableViewController.m或我的CustomCell.m?如果你不介意,請給我提供代碼和解釋,即時通訊新的在這裏,仍然在學習。我想了解你編寫的每一行代碼。謝謝。

更新:

這是我的cellForRowAtIndexPath裏面TableViewController.m至今:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]]; 
    if (cell == nil) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     cell = _customCell; 
     _customCell = nil; 
    } 
    button.tag = indexPath.row; 
    cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row]; 
    cell.timerLabel.text = [timeArray objectAtIndex:indexPath.row]; 
    time = [[secondArray objectAtIndex:indexPath.row] integerValue]; 
    NSLog(@"Title %@, time %@, second %@, time %i, tag %d", titleArray, timeArray, secondArray, time, button.tag); 
    return cell; 
} 

,這是日誌說:

2012-08-04 17:38:36.257 Table[1269:f803] Title (
    "Test 1", 
    "Test 2" 
), time (
    "10 secs", 
    "5 secs" 
), second (
    10, 
    5 
), time 10, tag 0 // tag 0 for cell 1 
2012-08-04 17:38:36.261 Table[1269:f803] Title (
    "Test 1", 
    "Test 2" 
), time (
    "10 secs", 
    "5 secs" 
), second (
    10, 
    5 
), time 5, tag 0 // tag should be 1 for cell 2 but it said 0. How to fix it? 

回答

1

這是你如何添加按鈕與動作在單元格中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

// ADDING A BUTTON WITH TARGET 
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundRect]; 
btn.frame = CGRectMake(0, 0, 50, 50); 
btn.showsTouchWhenHighlighted = YES; 
btn.tag = indexPath.row; 
[btn addTarget:self action:@selector(onoffBtn:) forControlEvents:UIControlEventTouchUpInside]; 

[cell.contentView addSubview:btn]; 

return cell; 
} 


- (IBAction)onoffBtn:(id)sender 
{ 
UIButton *button = (UIButton *)sender; 
button.selected = !button.selected; 
if (button.selected) 
{ 
    // Do action 1 
} 
else 
{ 
    // Do action 2 
} 
} 
+0

謝謝你的快速回答,即時通訊初學者。我能問一些問題嗎?我已經在我的CustomCell中放置了一個按鈕,我是否應該再次使用您的代碼添加一個按鈕?非常感謝你 – Piyo 2012-08-04 08:02:40

+1

,你可以刪除按鈕,添加此代碼,這將根據框架設置動態添加一個按鈕,所有的細胞,或者如果你的細胞是恆定的,你有你自己通過廈門國際銀行,你可以簡單地連接同一添加按鈕IBAction爲在廈門國際銀行文件中的所有按鈕 – superGokuN 2012-08-04 08:05:46

+0

先生,我已經刪除,但我可以用我的代碼連接我的按鈕,我把button.tag = indexPath.row;在我的cellForRowAtIndexPath和NSLog中。但是當我用我的tableview中的2個單元格運行它時,NSLog顯示的兩個標籤號都是0,而不是0和1.我忘記了什麼嗎?請教,如何讓我的按鈕可以設置正確的標籤? – Piyo 2012-08-04 09:53:44

相關問題