更改UITableView中创建的UIButton的图像

时间:2011-09-08 11:08:50

标签: iphone uitableview uibutton

我在UIbutton内创建了UITableView。我有多行。在触摸特定行的按钮时,我希望更改该特定按钮的图像。我添加的按钮是一个复选框。所以每当我触摸按钮时,它的图像都会改变。

这是我的代码:

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

/*  

Some code  

*/  

 //Checkbox
        UIImage *buttonImage = [UIImage imageNamed:@"arrow-not-checked.png"];
        btnConfirm = [UIButton buttonWithType:UIButtonTypeCustom];
        btnConfirm.tag =[[arrID objectAtIndex:indexPath.row] intValue]; 
        [btnConfirm setImage:buttonImage forState:UIControlStateNormal];
        btnConfirm.frame = CGRectMake(checkboxXX, 10.0, buttonImage.size.width, buttonImage.size.height);
        [btnConfirm addTarget:self action:@selector(AddToCart:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btnConfirm];  

}  



-(void)AddToCart:(id)sender
{
    UIButton *btnTemp=sender;
    int iTag = btnTemp.tag;  

    // I want to change image of sender

}  

3 个答案:

答案 0 :(得分:3)

-(void)AddToCart:(id)sender

{
UIButton *btnTemp = (UIButton *)sender;
int iTag = btnTemp.tag;  

// I want to change image of sender
[btnTemp setImage:yourImage forState:UIControlStateNormal];

}  

试试这个。希望这会有所帮助。

答案 1 :(得分:2)

这个怎么样:

-(void)AddToCart:(UIButton *) addButton
{
   [addButton setImage:yourImage forState: UIControlStateNormal];
}  

发送对象的类型不必是id

答案 2 :(得分:1)

试试这个......

-(void)AddToCart:(id)sender

{
UIButton *btnTemp = (UIButton *)[yourTableView viewWithTag:sender];
int iTag = btnTemp.tag;  

// I want to change image of sender
[btnTemp setImage:yourImage forState:UIControlStateNormal];

}