ios - tableview按钮点击需要在ios中使用pickerview

标签 ios iphone

在 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;
    }

}

此代码仅适用于最后一个单元格。想要在 TableView 中的所有单元格中工作

enter image description here

最佳答案

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

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

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

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

您再次尝试在以下方法的外部访问 myPickerView

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

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

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

myButtoncmt.tag = indexpath.row;

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

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

现在 - (IBAction)picker_select:(id)search 使用以下代码访问

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;

现在您可以看到所有选择器。试试让我知道你的结果

关于ios - tableview按钮点击需要在ios中使用pickerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26607239/

相关文章:

iphone - iOS + 自动布局 : UITableViewCell label + textfield width with rotation

iphone - 覆盖保存在 NSUserDefaults 中的值

ios - PodFile中 'GoogleCloudMessaging'和 'Google/CloudMessaging'之间的区别

ios - iOS Safari忽略HTML的“maxlength”属性

ios - 找不到 Firebase 模块 "Objective C Project"

ios - 将图像划分为可点击的部分

objective-c - iOS、objective C - 没有 XIB 和 MVC 的应用程序的好例子

iphone - 我应该调用 removeFromSuperview 还是 release 就好了?

iphone - 如何同时使用 CGAffineTransformMakeScale 和 Rotation?

c++ - iPhone OpenGL ES 2.0 渲染到纹理会乘以颜色值而不是相加