当我滚动UITableViewCell时,在iOS中选择的图像更改之前

时间:2014-07-19 11:39:58

标签: ios objective-c

我需要实现这个功能。请帮助我。 在“unblockbtnClick”之后单击更改图像,没关系,但滚动查看时,所选图像都将更改为以前的图像。

这是我的代码。

  // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {


        static NSString *cellIdentifier = @"BlockedCell";


        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if(cell == nil)
        {
            NSArray *nib;
                nib = [[NSBundle mainBundle] loadNibNamed:@"BlockedCell" owner:self options:nil];
             cell = [nib objectAtIndex:0];


        }
        [btn_tblvcAddbtn addTarget:self action:@selector(unblockbtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn_tblvcAddbtn setTag:indexPath.row];
            return cell;
    }

    -(IBAction)unblockbtnClick:(UIButton *)sender{

        NSLog(@"Value of selected button = %ld",(long)[sender tag]);
        [sender setBackgroundImage:[UIImage imageNamed:@"remove.png"] forState:UIControlStateNormal];
    }

3 个答案:

答案 0 :(得分:1)

您需要在if条件

之外分配nib文件
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        // initialize cell here
    }

   NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BlockedCell" owner:self options:nil];
   cell = [nib objectAtIndex:0];

答案 1 :(得分:0)

-(IBAction)unblockbtnClick:(id)sender
{
    UIButton *btn=(UIButton *)sender;
    UITableViewCell *cellll;
    if([[[UIDevice currentDevice] systemVersion] intValue]<6)
        cellll=(UITableViewCell *)[btn superview];
    else
        cellll=(UITableViewCell *)[[btn superview]superview];



    NSIndexPath *aaa=[tblOption indexPathForCell:cellll];


    if([ArrSelectedData containsObject:[TblDataArrr objectAtIndex:aaa.row]])
    {
//tblDataArr is table dataArray  && ArrSelectedData is a array which contain selected itme of table 

        [ArrSelectedData removeObject:[TblDataArrr objectAtIndex:aaa.row]];
        [btn setBackgroundImage:[UIImage imageNamed:@"blank_checkbox.png"] forState:UIControlStateNormal];
    }
    else
    {
      [ArrSelectedData addObject:[TblDataArrr objectAtIndex:aaa.row]];
        [btn setBackgroundImage:[UIImage imageNamed:@"tick_checkbox.png"] forState:UIControlStateNormal];
    }

}

,这在cellForRowAtIndexPath

{
 if([ArrSelectedData containsObject:[TblDataArrr objectAtIndex:indexPath.row]])
          [tempBtn setBackgroundImage:[UIImage imageNamed:@"tick_checkbox.png"] forState:UIControlStateNormal];
    else
        [tempBtn setBackgroundImage:[UIImage imageNamed:@"blank_checkbox.png"] forState:UIControlStateNormal];

}

答案 2 :(得分:0)

当您将一个单元格滚出屏幕然后再调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法时。你没有在那里设置背景,这就是重置的原因。

要解决此问题,您需要存储有关所选单元格的信息,并在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中进行检查并相应地应用适当的背景。