取消选择行上的按钮

时间:2013-07-18 02:15:29

标签: ios uitableview uiviewcontroller uibutton

viewcontroller A转到B,然后回到A.我在viewcontrollerA的UItableview的每个单元格中都有一个UIButton。单击按钮时,按钮突出显示并转到viewcontrollerB。当你回到viewcontrollerA时,我希望取消选择按钮。 按钮的突出显示效果很好,即取消选择是问题。

在cellforrowatindexpath中,我有这个:

        if(indexPath.row==selectedbuttonindex)
        {
            addpeople.selected=YES;
        }
        else
        {
            addpeople.selected=NO;
        }

            UIImage *buttonImageNormal =[UIImage imageNamed:@"addtochatNormal.png"];
                 UIImage *buttonImageSelected =[UIImage imageNamed:@"addtochatSelected.png"];
           [addpeople addTarget:self action:@selector(addpeopletoChat:)
            forControlEvents:UIControlEventTouchDown];


                [addpeople setBackgroundImage:buttonImageSelected     forState:UIControlStateHighlighted];


                 [addpeople setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
                  [addpeople setBackgroundImage:buttonImageSelected forState:UIControlStateSelected];

                  addpeople.frame = CGRectMake(0,0, 51, 44);

                  [cell.contentView addSubview:addpeople];

使用以下方法选择按钮和更改视图控制器:

  -(void) addpeopletoChat : (UIButton*)button{



       UITableViewCell *cell_ = (UITableViewCell *)button.superview.superview;
       NSIndexPath *index_Path = [topictableView indexPathForCell:cell_];
       NSLog(@"%@", index_Path);
   selectedbuttonindex=index_Path.row;
  [topictableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:index_Path]    withRowAnimation:UITableViewRowAnimationFade];
   countaddpeople=countaddpeople+1;

  addpeopletochatViewController* viewcontrollerB = [[addpeopletochatViewController alloc]    init];
   viewcontrollerB.viewcontrollerA=self;

   [self.slidingViewController anchorTopViewTo:ECRight];//changes view to viewcontroller B


  }

这应该取消选择

  -(void) somefuction{

  NSLog(@"%d", selectedbuttonindex);
   NSUInteger indexArr[] = {0,selectedbuttonindex};
   NSIndexPath *index_Path = [NSIndexPath indexPathWithIndexes:indexArr length:2];
   selectedbuttonindex=-1;
   NSLog(@"%@" @"%@", @"hi", index_Path);

   [topictableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:index_Path] withRowAnimation:UITableViewRowAnimationFade];

   }

在viewcontroller B的h文件中我有:

  #import <UIKit/UIKit.h>
  #import "officerTopicsViewController.h"

  @class officerTopicsViewController;
  @interface addpeopletochatViewController : UIViewController{


  }
  @property (nonatomic,retain) officerTopicsViewController *viewcontrollerA;

  @end

并在其m文件中我有:

 -(void)viewDidDisappear:(BOOL)animated
  {

 [viewcontrollerA somefunction];


  }

但是它仍然不起作用

注意:我不能把selectedbuttonindex = -1;因为视图控制器的工作原理而出现在视图中

提前致意并表示感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用的东西(虽然我不知道这是否是最好的方法)是在viewDidLoad方法加载视图时设置按钮的默认图像。所以只是:

-(void)viewDidLoad
{
    //Whatever you have in here...
     yourButtonName setImage: [UIImage imageNamed:@"addtochatNormal.png"];
    [super viewDidLoad];
}

或者那些东西。

如果我没有问你的问题,请告诉我。