【问题标题】:Tableview button click need pickerview in iosios中的Tableview按钮单击需要pickerview
【发布时间】:2014-10-28 20:23:10
【问题描述】:

在 tableview 按钮单击中创建 UIpickerview。当第一次单击每个按钮时,选择器要显示。如果我在另一个时间单击相同的按钮,选择器想要隐藏并获取选择器值。在这里我只把我的代码放在最后一个单元格索引完成的过程第一个单元格索引不起作用?我该如何解决这个问题帮助我!!!这里屏幕! 在全局中创建选择器

UIPickerView *myPickerView;` 和 tableview 中的访问

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_arr_tags count]; /// in tag array number of button count
}

-(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:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;


         /// create a button

        UIImage *ButtonImagecmt = [UIImage imageNamed:@"orangeButton@2x.png"];
        UIButton *myButtoncmt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        myButtoncmt.frame = CGRectMake(10,50,90,35);
        [myButtoncmt setBackgroundImage:ButtonImagecmt forState:UIControlStateNormal];
        [myButtoncmt addTarget:self action:@selector(picker_select:)forControlEvents:UIControlEventTouchDown];

        [cell addSubview:myButtoncmt];

        flg=1; /// first time picker want to hide so flag set 1.




        myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(120, 0, 160, 180)];
        myPickerView.delegate = self;
        myPickerView.showsSelectionIndicator = YES;
        myPickerView.hidden=true;
        [cell addSubview:myPickerView];

    }


    return cell;
}
- (IBAction)picker_select:(id)search
{
    if (flg==1)
    {
        /// first time button clickt picker want to display
        myPickerView.hidden=false;
        flg=0;
    }
    else
    {
        /// secound time button click picker want to hide 
        myPickerView.hidden=true;
        flg=1;
    }

}

此代码仅在最后一个单元格中有效。想要在表格视图中的所有单元格中工作

【问题讨论】:

  • 而不是检查'flg'使用按钮选择状态。我希望taht会更好。在这里它不起作用,因为所有单元格只有一个“flg”变量。
  • 如何检查表格视图单元格中每个按钮单击的 flg。如何在 tableview 中的每个单元格按钮中隐藏和显示pickerview 帮助我。
  • 在您的 IBAction 中尝试重新加载 tableView,因为目前您正在设置 myPickerView.hidden 但我看不到您正在刷新 tableView。希望这会有所帮助。
  • 我想在每次单击单元格按钮时隐藏选择器。如果我点击第一个单元格按钮最后一个单元格选择器隐藏和显示它的工作最后一个单元格..每个单元格按钮点击选择器想要隐藏和显示..我怎样才能获得帮助???
  • tabeview relaod 不工作

标签: ios iphone


【解决方案1】:

首先让我解释一下你的错误,然后我会建议你用其他方式。 您正在声明 UIPickerView *myPickerView;下面方法的外侧

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

但是您正在以下方法的 inside 初始化 myPickerView

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

您又一次尝试在以下方法之外访问 myPickerView

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

只是对象 myPickerView 代表最后一个单元格选取器视图。因此,无论您在表格视图中点击了什么按钮,myPickerView 始终代表最后一个单元格选取器视图。根据可重用性概念,在加载屏幕上的最后一个单元格之前,您无法看到此选择器。 所以我建议你使用你的代码

在 cellForRowAtIndexPath 方法中将索引行设置为按钮的标签,如下所示

myButtoncmt.tag = indexpath.row;

现在以同样的方式将标签设置为您的pickerview(我不推荐这样做,但我正在尝试解决您的代码问题),如下所示

myPickerView.tag = [_arr_tags count]+indexpath.row;

现在 - (IBAction)picker_select:(id) 使用以下代码搜索访问权限

UIButton *tempBtn =(UIButton *) search;
UITableViewCell *cell= [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tempBtn.tag inSection:0]]
UIPickerView *pickerView = [cell viewWithTag:[_arr_tags count]+tempBtn.tag];
pickerView.hidden = !pickerView.isHidden;

现在您可以看到所有选择器。试着告诉我你的结果

【讨论】:

  • 如果我单击第一个单元格按钮,选择器要显示。我再次单击选择器要隐藏的相同按钮。这可以通过 tableview 中的所有单元格来完成。
  • 想创建标签并设置标签值的方法一样
  • 对不起,我没明白你的问题。请描述您的问题
  • cellForRowAtIndexPath 想要创建一个标签。如果我单击按钮,但是选择器会隐藏和显示。同样地标签想要隐藏和显示在按钮点击。在该标签中要显示pickerview选定的对象。
猜你喜欢
  • 2018-05-22
  • 1970-01-01
  • 1970-01-01
  • 2018-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多