1

彼女の tableAllconacts は私の 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];

}
4

4 に答える 4

0

それはあなたを助けるかもしれません。

条件にこれ if(cell == nil)を書きます。

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;
[cell.contentView addSubview:btnChk]; //added this line to your code.

if 条件の外側で、タグを使用してこのように同じボタンを取得します

UIButton *btn2 = (UIButton *)[cell.contentView viewWithTag:indexPath.row];

このボタンに画像を適用します。

于 2013-08-19T05:26:18.907 に答える
0
  1. 背景画像を変更するコード行は、投稿したコードでコメント化されています。

  2. メソッド内にボタンを作成しています。

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

これにより、テーブルをリロードするたびにボタンが描画されます。私の提案は、上記のメソッドの外でボタンを作成し、メソッド内にタグを設定することです。

于 2013-08-19T05:16:01.097 に答える