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

当我滚动UITableViewCell时,在iOS中更改选定图像之前,ios,objective-c,Ios,Objective C,我需要实现此功能。请帮助我。 在“UnbobTnclick”之后,单击更改图像,这是正常的,但是当滚动tableview时,所选图像都将更改为以前的图像 这是我的密码 // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

我需要实现此功能。请帮助我。 在“UnbobTnclick”之后,单击更改图像,这是正常的,但是当滚动tableview时,所选图像都将更改为以前的图像

这是我的密码

  // 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];
    }

如果条件,则需要在
之外分配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];

当您将单元格从屏幕中滚动出来,然后返回时,将调用
-(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath
方法。你没有在那里设置背景,这就是它被重置的原因


要修复此问题,您需要存储有关选定单元格的信息,并将其签入
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
,并相应地应用适当的背景。

好的,您可以为此提供代码吗。当您滚动时,然后重新绘制您创建的每个单元格。我将此代码,您在上面给出的。这不起作用。您能否对此给出另一个答案。按钮图像在滚动后更改但不保存。您使用ArrSelectedData数组。。不要忘记在viewdidload方法中分配它
-(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];
    }

}
{
 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];

}