【问题标题】:getting element from array one by one and createbutton从数组中一一获取元素并创建按钮
【发布时间】:2016-12-01 07:38:40
【问题描述】:

我有一个 UItextFeild 作为搜索栏,点击打开了建议的 tableview。现在我想点击该行并在下面的视图中创建一个带有标签的按钮,如图所示。我怎样才能实现这个

我想要一个一个,当我点击行时,它会在视图上创建一个按钮并隐藏 UITableView 然后我再次开始搜索并再次点击其他行创建一个具有不同 X 值的按钮。

 _selectednames = [NSMutableArray arrayWithCapacity:self.sortedArray.count];


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
       {
           // MGContactItemsModel *model = _arrayForContacts[indexPath.row];
    self.searchText.text = [NSString stringWithFormat:@"%@",[_sortedArray  objectAtIndex:indexPath.row]];
self.tableView.hidden = YES;

[_selectednames addObject:_searchText.text];

for (int i; i < _selectednames.count ; i++) {
    [self makeLabelsAndButtons:i];

       }

现在在我的 makeLabelsAndButtons 函数中我做了这样的事情

CGFloat xOffset = 10.0f;
int buttonCount = 0;

CGRect buttonFrame = CGRectMake(xOffset, 10.0, 50.0, 15.0);

NSString *nameString = [self.selectednames objectAtIndex:indexPath];

     UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
   //_button.frame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
   for(int i=buttonCount; i >0;i++) {

    xOffset = xOffset+10;


    buttonFrame.origin.x += buttonFrame.size.width+xOffset;





}
 _button.frame = buttonFrame;
   buttonCount++;


_button.tag = indexPath;



//button.center = CGPointMake(xOffset, 10.0f);
//[button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
[_button setTitle:self.selectednames[indexPath] forState:UIControlStateNormal];
_button.backgroundColor = [UIColor lightGrayColor];
    _button.titleLabel.font = [UIFont fontWithName:@"Arial" size:8.0f];

    _button.layer.cornerRadius = 10.0f;
    _button.layer.masksToBounds = YES;

[self.buttonView addSubview:_button];
NSLog(@"name: %@", nameString);

在点击按钮时它也会被删除。任何帮助将不胜感激。

【问题讨论】:

    标签: ios objective-c arrays uitableview uibutton


    【解决方案1】:

    它对我有用。请尝试一下。

     y=20;
     x=20;
    
                for (int i=0; i<[arrAssignUsers count]; i++) {
                    CGRect screenRect=[[UIScreen mainScreen]bounds];
                    CGFloat screenWidth=screenRect.size.width;
    
    
                    [arrBtnStatus addObject:[NSNumber  numberWithBool:NO]];
    
                    NSString *strNames=[arrAssignUsers objectAtIndex:i];
                    stringsize=[strNames sizeWithAttributes:
                                @{NSFontAttributeName: [UIFont systemFontOfSize:20.0f]}];
                    btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
                    CGFloat m=x+stringsize.width+10;
                    CGFloat n=screenWidth-20;
    
                    if (m<=n) {
                        btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
                        x=x+stringsize.width +10;
                    }
                    else
                    {
                        y=y+stringsize.height+10;
                        x=20;
                        btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
                        x=x+stringsize.width+10;
                    }
    
                    [btn setTitle:self.arrAssignUsers[i] forState:UIControlStateNormal];
                    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    btn.tag=i;
                    [btn addTarget:self
                            action:@selector(notifyButtonPressedInEditTask:) forControlEvents:UIControlEventTouchUpInside];
    
                    btn.backgroundColor=WHITE_COLOR;
                    btn.layer.cornerRadius=10;
                    [btn.layer setMasksToBounds:YES];
                    btn.layer.borderWidth=2.0f;
                    btn.layer.borderColor=[UIColor orangeColor].CGColor;
    
                    [self checkNotifybtnStatus:btn];
                    [cell addSubview:btn];
                }
            }
    

    这可能对你有帮助。谢谢

    【讨论】:

    • 什么是 arrBtnstaus 和 stringSize ,我的意思是你为什么使用这个数组..?你能解释一下吗
    • 请按向上箭头
    • 实际上我们创建的按钮在每次新创建按钮时都会一次又一次地变成......该怎么办..?
    • 没有按钮 == 数组计数?如果你想在每次点击行时创建多个按钮,那么首先从数组中删除所有对象然后添加
    • 意思是我不明白。请解释一下哥们。谢谢
    【解决方案2】:

    您可以使用 UICollectionView 创建按钮。你需要做的就是 1.根据需要创建带有按钮的自定义单元格 2.当您点击表格视图单元格时,将项目添加到数组并重新加载 UICollectionView 3. 显示按钮,它会自动为你添加滚动视图。

    当您点击 UICollectionView 单元格时,从数组中删除对象并重新加载 UICollectionView。 与添加按钮和设置框架相比,这将更容易。

    并以与文本长度相同的方式设计 UICollectionView 单元格,为此使用 NSLayoutConstraints。它将快速而流畅地工作。 代码将是最少且可重复使用的。

    【讨论】:

    • 你能给我一些实际上是 ios 开发新手的代码吗?无论如何谢谢你的帮助。
    • 我不能提供完整的代码,但我肯定可以为您提供一些参考链接。这些是:github.com/keighl/KTCenterFlowLayout 使用它,您可以根据文本创建具有动态宽度和高度的 tableview 单元格。
    【解决方案3】:

    试试这个工作代码... 将 UIScrollView 放在 Xib 中作为按钮

    //Adding menu buttons
        btnArray = @[@"SYMPTOMATIC", @"SOCIAL", @"VOCATIONAL", @"PERSONAL"];
        [self addButtonsInScrollMenu:btnArray];
    
    
    /**
     *  Add Menu Buttons in Menu Scroll View
     *
     *  @param buttonArray Array containing all menu button title
     */
    
    #pragma mark - Add Menu Buttons in Menu Scroll View
    -(void) addButtonsInScrollMenu:(NSArray *)buttonArray
    {
        CGFloat buttonHeight = self.menuScrollView.frame.size.height;
        CGFloat cWidth = 0.0f;
    
        for (int i = 0 ; i<buttonArray.count; i++)
        {
            NSString *tagTitle = [buttonArray objectAtIndex:i];
            CGFloat buttonWidth = [self widthForMenuTitle:tagTitle buttonEdgeInsets:kDefaultEdgeInsets];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(cWidth, 0.0f, buttonWidth, buttonHeight);
            [button setTitle:tagTitle forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont fontWithName:@"Roboto-Regular" size:12.0f];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            //[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
            [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = i;
            [self.menuScrollView addSubview:button];
    
    
            UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, button.frame.size.height - kButtonBorderHeight, button.frame.size.width, kButtonBorderHeight)];
            bottomView.backgroundColor = [UIColor darkTextColor];
            bottomView.tag = 1001;
            [button addSubview:bottomView];
    
    
            if (i == 0)
            {
                button.selected = YES;
                [bottomView setHidden:NO];
            }
            else
            {
                [bottomView setHidden:YES];
            }
    
            cWidth += buttonWidth;
        }
    
        NSLog(@"scroll menu width->%f",cWidth);
        self.menuScrollView.contentSize = CGSizeMake(cWidth, self.menuScrollView.frame.size.height);
    }
    

    【讨论】:

    • 我的数组元素在点击 tableview 行时增加
    • 只需在每个按钮单击时在数组中添加新元素并调用'addButtonsInScrollMenu'方法。
    • 在 didSelectRowAtIndexPath [btnArray addObject:@"row 5"];
    • 并调用 [self addButtonsInScrollMenu:btnArray];
    • 或者你可以在每次选择任何表格行时只传递一个字符串而不是 NSArray。
    猜你喜欢
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    相关资源
    最近更新 更多