【问题标题】:Creating Button According to Search Through UITextField in ios在ios中通过UITextField搜索根据搜索创建按钮
【发布时间】:2016-12-19 08:43:08
【问题描述】:

我想根据用户在 tableView 上点击创建按钮,我使用了这种方法,但它会与我的按钮重叠,这意味着当创建第二个按钮时,它也会在第一个按钮上创建一个按钮,我该如何实现。按钮创建将是一对一的,而不是多选。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{

self.searchText.text = [NSString stringWithFormat:@"%@",[_sortedArray objectAtIndex:indexPath.row]];
self.tableView.hidden = YES;

 [_selectednames addObject:_searchText.text];

    [self makeLabelsAndButtons:_selectednames];

   // }

self.searchText.text = nil;

 }


 -(void)makeLabelsAndButtons:(NSMutableArray *)arrButton{

 int y=10;
 int x=10;

for (int i=0; i<[arrButton count]; i++) {
    CGRect screenRect=[[UIScreen mainScreen]bounds];
    CGFloat screenWidth=screenRect.size.width;

  //  [_arrBtnStatus addObject:[NSNumber  numberWithInt:i]];


    NSString *strNames=[arrButton objectAtIndex:i];
   CGSize stringsize=[strNames sizeWithAttributes:
                @{NSFontAttributeName: [UIFont systemFontOfSize:12.0f]}];
   UIButton  *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    CGFloat m=x+stringsize.width+30;
    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:arrButton[i] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.tag=i;
    [btn addTarget:self
            action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    btn.backgroundColor = [UIColor lightGrayColor];

   btn.layer.cornerRadius=10;
   [btn.layer setMasksToBounds:YES];

    [self.buttonView addSubview:btn];
}

【问题讨论】:

  • 用你的输出更新你的问题你想要什么和你得到什么。
  • 请向我们提供更多信息。您不想在哪里看到新按钮?为什么你需要不同的按钮。快速草稿也是个好主意。
  • 我已经用图片编辑了我的问题......现在提出一些建议
  • 任何人请帮助我

标签: ios uitableview uibutton


【解决方案1】:

每当 indexpath 处的行单元格运行时,请初始化 selectednames 数组。即在此函数中初始化数组。这样,它将根据所选行仅添加一项。 在您的代码中,您将对象添加到同一个数组中。这就是它添加上一个按钮的原因

【讨论】:

  • [_selectednames addObject:_searchText.text];
  • 每当用户点击单元格时,您都会添加到这个数组中。并在此基础上创建按钮。所以基本上你总是在数组中添加一些东西。只需在 cellforrow 代码的顶部初始化这个数组它会很好,因为它总是会在用户点击单元格时创建一个新数组
  • 您只想通过点击单元格来创建一个按钮?
猜你喜欢
  • 1970-01-01
  • 2020-02-27
  • 1970-01-01
  • 2019-04-06
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多