简体   繁体   English

如何在UITableViewCell中使用Button执行操作?

[英]How to do action using Button in UITableViewCell?

i have a button that i create in my CustomCell. 我有一个在CustomCell中创建的按钮。

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

When i run it, the button is show in my cell inside UITableView that i create in TableViewController. 当我运行它时,该按钮显示在我在TableViewController中创建的UITableView内的单元格中。 I can press that onoffBtn inside my cell but it cant do action. 我可以将onoffBtn按下我的单元格,但它无法执行操作。 How do i do so that my button can do action? 我该如何做才能使我的按钮起作用?

this is my cellForRowAtIndexPath inside my TableViewController.m 这是我的TableViewController.m中的cellForRowAtIndexPath

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

and for other question, where should i write for do process at? 还有其他问题,我应该在哪里写流程? inside my TableViewController.m or my CustomCell.m? 在我的TableViewController.m或CustomCell.m中? if you dont mind, please provide me with code and explanation, im new in here and still learning. 如果您不介意,请向我提供代码和说明,此处是新内容,并且仍在学习中。 i want to understand in every line of code that you write. 我想了解您编写的每一行代码。 Thank you. 谢谢。

Update: 更新:

This is my cellForRowAtIndexPath inside TableViewController.m so far: 到目前为止,这是我在TableViewController.m中的cellForRowAtIndexPath:

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

and this is log said: 这是日志说:

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?

THIS IS HOW YOU ADD BUTTON WITH AN ACTION IN A CELL 这是您如何在细胞中添加按钮的动作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM