重新加载表视图中的按钮中的图像

时间:2011-06-10 09:54:13

标签: objective-c cocoa-touch ipad uitableview

在我的表视图中,我有4 UIButton个,每个表视图中有2个。最初所有按钮上都有一个图像(它们是硬编码的)。现在我的应用程序中有一个按钮,当点击时,使用setImage方法用新的按钮替换按钮上的图像,然后重新加载表格视图。但不幸的是,我得到的旧表视图只是我的意思与旧图像。如何在按钮上获取新图像?

我的cellForRowAtIndexPath实现: - //自定义表格视图单元格的外观。

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // if (cell == nil) {
    // cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    // }
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    if(indexPath.row == 0)
    {
        button1=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
        [button1 setImage:[UIImage imageNamed:@"BUTTON.png"] forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:self.button1];

        button2=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
        [button2 setImage:[UIImage imageNamed:@"TURN OFF.png"] forState:UIControlStateNormal];
        [button2 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button2];
    }
    else if(indexPath.row == 1)
    {
        button3=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
        [button3 setImage:[UIImage imageNamed:@"JUMP.png"] forState:UIControlStateNormal];
        [button3 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button3];


        button4=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
        [button4 setImage:[UIImage imageNamed:@"CLOSE.png"] forState:UIControlStateNormal];
        [button4 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button4];
    }
    else
    {
        // Do something here
    }
    return cell;
}

2 个答案:

答案 0 :(得分:0)

您可能将图像设置为错误的状态:

[myButton setImage:[UIImage imageNamed:@"myImage.png"] forState:UIControlStateNormal];

您需要为UIControlStateNormal设置正确的值 - 其中正确表示与按钮当前状态匹配的值。

答案 1 :(得分:0)

看起来你在单元格中有行方法的硬编码图像,所以每当你重新加载你的tableview时,它们都会有相同的图像。

所以如果你想改变它们,那么拿一个布尔变量并像下面那样更新它:

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

    static NSString * CellIdentifier = @“Cell”;

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // if(cell == nil){ // cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; //} if(cell == nil){     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; }

    if(indexPath.row == 0) {     button1 = [[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];

    [button1 setImage:[UIImage imageNamed:@"BUTTON.png"] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:self.button1];
    
    button2=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
    [button2 setImage:[UIImage imageNamed:@"TURN OFF.png"] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button2];
    

    } else if(indexPath.row == 1) {     button3 = [[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];     [button3 setImage:[UIImage imageNamed:@“JUMP.png”] forState:UIControlStateNormal];     [button3 addTarget:self action:@selector(ClickTo :) forControlEvents:UIControlEventTouchUpInside];     [cell.contentView addSubview:button3];

    button4=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
    [button4 setImage:[UIImage imageNamed:@"CLOSE.png"] forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button4];
    

    } 其他 {     //在这做点什么 }

如果(myflag)               {

[button1 setImage:[UIImage imageNamed:@“changedImage1”] forState:UIControlStateNormal];  [button2 setImage:[UIImage imageNamed:@“changedImage2”] forState:UIControlStateNormal];  [button3 setImage:[UIImage imageNamed:@“changedImage3”] forState:UIControlStateNormal];  [button4 setImage:[UIImage imageNamed:@“changedImage4”] forState:UIControlStateNormal];

}

return cell;

}

//在Click to Method中设置myflag yes并重新加载表

- (void)ClickTo:(IBAction)发件人 {

myflag = YES;   [tableview reloadData];

}