在iOS应用程序中的UITableViewCell内按下时无法更改按钮图像

时间:2013-01-07 23:32:43

标签: ios uitableview uibutton

我有一个UITableViewController,它包含一个表格,我希望它的单元格添加一个按钮。这是一个自定义按钮,用作复选框,其中包含正常状态的图像,以及用户选择它时的另一个图像。不幸的是,每当我按下它时图像都不会改变状态,并且出于某种原因,它也没有调用目标方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    // Configure the cell...
    TestObject *tObject = [myNSMutableArray objectAtIndex:indexPath.row];
    cell.textLabel.text = tObject.testTitle;

    testButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [testButton setFrame:CGRectMake(280, 57, 25, 25)];
    [testButton setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateSelected];
    [testButton setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
    [testButton setUserInteractionEnabled:YES];
    [testButton addTarget:self action:@selector(clickOnCheckButton:) forControlEvents:UIControlEventTouchUpInside];

    [cell addSubview:testButton];

    return cell;

}

为了记录,我确实实现了这个方法:

-(void)clickOnCheckButton:(id)sender {

    NSLog(@"button has been pressed");
}

并确保拼写正确,这就是为什么我不理解在按下每个单元格中的按钮时没有调用该方法的原因。

我的表输出的另一个问题是我的第一个单元格里面没有按钮。但是,它下面的所有其他单元格都可以。

谁能看到我做错了什么?

感谢所有回复的人。

2 个答案:

答案 0 :(得分:0)

如果您使用单元格的accessoryView属性,则可以将其设置为自定义视图,例如按钮,它的设计就像按钮一样。然后,您可以选择使用UITableViewDelegate方法– tableView:accessoryButtonTappedForRowWithIndexPath:来控制操作。

答案 1 :(得分:0)

您正在我们的项目中编写流动的代码

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


    ObjectiveCcustTcell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
   ObjctiveString *temp=[_arraymain objectAtIndex:indexPath.row];


    cell.labQutionNo.text=temp.qutNO;
    cell.labQution.text=temp.qution;
    cell.labAns.text=temp.ans;
    cell.labAns2.text=temp.ans2;
    cell.labAns3.text=temp.ans3;

[cell.ButAns1 setBackgroundImage:[UIImage imageNamed:temp.ansD1] forState:UIControlStateNormal];
[cell.ButAns2 setBackgroundImage:[UIImage imageNamed:temp.ansD2] forState:UIControlStateNormal];
[cell.ButAns3 setBackgroundImage:[UIImage imageNamed:temp.ansD3] forState:UIControlStateNormal];

     if(indexPath.row==0&&t==1)
        {
             [cell.ButAns1 setBackgroundImage:[UIImage imageNamed:@"select"] forState:UIControlStateNormal];
        }

    if(indexPath.row==1&&t==3)
            {

             [cell.ButAns1 setBackgroundImage:[UIImage imageNamed:@"select"] forState:UIControlStateNormal];
            }



    [cell.ButAns1 addTarget:self action:@selector(MEthodBut:)forControlEvents:UIControlEventTouchUpInside];
    [cell.ButAns2 addTarget:self action:@selector(MEthodBut2:) forControlEvents:UIControlEventTouchUpInside];
     [cell.ButAns3 addTarget:self action:@selector(MEthodBut3:) forControlEvents:UIControlEventTouchUpInside];

     [cell.ButAns1 setTag:indexPath.row];
     [cell.ButAns2 setTag:indexPath.row];
    [cell.ButAns3 setTag:indexPath.row];

    return cell;



}


-(void)MEthodBut:(UIButton *)sender
{

    UIButton *button = (UIButton *)sender;
    int row = (int)button.tag;





    if(row==0)
    {

        t=1;

[_tablemain reload];

    }
}
相关问题