【问题标题】:deselecting buttons on rows取消选择行上的按钮
【发布时间】:2013-07-18 02:15:29
【问题描述】:

viewcontroller A 转到 B,然后返回 A。我有一个 UIButton,在 viewcontrollerA 的 UItableview 的每个单元格中。当您单击该按钮时,该按钮突出显示并转到 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;由于我的视图控制器的工作方式,in view 会出现

提前致谢。

【问题讨论】:

  • 再试一次,我明白了……是否调用了某个函数?您是否尝试重新加载整个表,而不是仅仅尝试重新加载旧的选定索引?
  • 再次感谢,您的解决方案有效,这又是因为我如何更改视图控制器

标签: ios uitableview uiviewcontroller uibutton


【解决方案1】:

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

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

或者类似的东西。

如果我没有正确回答您的问题,请告诉我。

【讨论】:

    猜你喜欢
    • 2012-02-21
    • 2018-02-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2011-07-17
    • 2021-09-01
    • 1970-01-01
    • 2015-07-01
    相关资源
    最近更新 更多