如果一旦设置,则无法更改 uiTableview 中按钮的背景图像 [英] Unable to change the background image of the button in uiTableview if once set

查看:20
本文介绍了如果一旦设置,则无法更改 uiTableview 中按钮的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

她的 tableAllconacts 是我的 nsmutablearray

Her tableAllconacts is my nsmutablearray

- (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= UITableViewCellSeparatorStyleNone;


        btnChk =[UIButton buttonWithType:UIButtonTypeCustom];
        btnChk.frame =CGRectMake(280, 4, 32, 32);
        btnChk.backgroundColor =[UIColor clearColor];
        [btnChk addTarget:self action:@selector(btnCheckmarkClicked:) forControlEvents:UIControlEventTouchUpInside];
        btnChk.tag=indexPath.row;


        UIImageView *separator=[[UIImageView alloc ]initWithFrame:CGRectMake(0, 44, 320, 1)];
        separator.image=[UIImage imageNamed:@"divider.png"];
        [cell.contentView addSubview:separator];

    }

    cell.textLabel.font = [UIFont systemFontOfSize:18];
    cell.textLabel.textColor =[UIColor whiteColor];
    cell.textLabel.text = [appdeleObj.arrDriverName objectAtIndex:indexPath.row];




    NSString *strValue=[appdeleObj.arrDriverName objectAtIndex:indexPath.row];

    NSLog(@"%@",strValue);

        if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"Checked"]isEqualToString:strValue] )

        {

            if ([[NSUserDefaults standardUserDefaults ] boolForKey:@"showChkmark"] )

            {

       [btnChk setBackgroundImage:[UIImage imageNamed:@"radio_on.png"] forState:UIControlStateNormal];            

        }
            else
            {

                [btnChk setBackgroundImage:[UIImage imageNamed:@"radio_off.png"] forState:UIControlStateNormal];

            }
        }
        else

        {
            NSLog(@"%@",btnChk.currentBackgroundImage);
            [btnChk setBackgroundImage:[UIImage imageNamed:@"radio_off.png"] forState:UIControlStateNormal];

        }
    [cell.contentView addSubview:btnChk];
    return cell;

}

按钮操作

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

  CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tableAllContacts];
  NSIndexPath *indexPath = [tableAllContacts indexPathForRowAtPoint:touchPoint];

// No need to use tag sender will keep the reference of clicked button
  UIButton *button = (UIButton *)sender;

    appdeleObj.strCheckUnchek =[appdeleObj.arrDriverName objectAtIndex:indexPath.row];

    if ([button.currentBackgroundImage isEqual:[UIImage imageNamed:@"radio_on.png"]])
    {
        [[NSUserDefaults standardUserDefaults] setValue:@"radio_off.png" forKey:@"Checked"];
     //   [button setBackgroundImage:nil forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"showChkmark"];

    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setValue:appdeleObj.strCheckUnchek forKey:@"Checked"];
   // [button setBackgroundImage:[UIImage imageNamed:@"radio_on.png"] forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"showChkmark"];


    }

    [tableAllContacts reloadData];

}

推荐答案

与其这样做,不如为按钮的不同状态设置两个背景图像,如下所示?

Instead of doing so, why don't you set the two background images for the different states of the button like below?

[button setBackgroundImage:[UIImage imageNamed:@"radio_on.png"]
                  forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:@"radio_off.png"]
                  forState:UIControlStateNormal];

然后你可以在 -(void)btnCheckmarkClicked: (UIButton *)sender 中反转 button.selected 字段的值;然后按钮会自动为你更新它的背景图片.

And then you can just invert the value of the button.selected field in -(void)btnCheckmarkClicked: (UIButton *)sender; then the button will update its backgroundImage automatically for you.

button.selected = !button.selected;

希望这会有所帮助.

这篇关于如果一旦设置,则无法更改 uiTableview 中按钮的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆