【问题标题】:Adding buttons dynamically to a uitableviewcell - iPhone app将按钮动态添加到 uitableviewcell - iPhone 应用程序
【发布时间】:2009-10-11 11:14:29
【问题描述】:

我有一个动词变位应用程序,它在表格的第一个单元格中显示动词翻译。目前翻译列表只是一个字符串(逗号分隔列表),但我想将其更改为具有可点击按钮。我曾尝试在单元格视图中添加按钮,但没有取得太大成功,但我对自定义单元格的唯一经验是使用特定定位,所以我不确定如何在单元格中实现按钮的动态列表(可变宽度)。

非常感谢任何帮助。

干杯。

【问题讨论】:

    标签: iphone dynamic uitableview variables uibutton


    【解决方案1】:

    我刚刚为我的新 iPad 应用做了这个,所以我想我应该粘贴代码。

    // NOTE - This is within a loop     
    // Add the translation as a button
        UIButton *translationButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
        translationButton.backgroundColor = [UIColor clearColor];
        translationButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [translationButton setTitle:translation forState:UIControlStateNormal];
        [translationButton addTarget:self action:@selector(translationSearch:) forControlEvents:UIControlEventTouchUpInside];
        // Work out required size
        fontSize = [translationButton.titleLabel.text sizeWithFont:translationButton.titleLabel.font];      
        CGRect buttonFrame = CGRectMake(xOffset, 15, fontSize.width + 20.0, 24);
        [translationButton setFrame:buttonFrame];
    
        // Now set the new x Offset
        xOffset = buttonFrame.origin.x + buttonFrame.size.width + 6.0;
    
        [verbView addSubview:translationButton];    
    

    干杯。

    【讨论】:

    • 如果我们没有按钮标题来区分按钮并且它们没有任何文本可以做什么。就我而言,我只是使用图像而不是按钮标题。遇到这种情况怎么办?
    • 如果您使用图像,那么我假设您知道它们的大小。在这种情况下,您可以简单地定期放置它们。上面的代码使用 fontSize.width 来计算偏移量。您应该能够简单地使用图像的宽度。
    【解决方案2】:

    关注

    -(CGRect)placeAButton:(NSString*)textFromField withCell:(UITableViewCell*)cell
    {
        CGSize      theSize;
        CGSize      constraintSize;
    
        // Create a button and add as subview to cell
        // Get coordinates to place the button
    
        UIButton *button    = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    
        button.titleLabel.font              = [UIFont systemFontOfSize:MEDIUM_FONT_SIZE];;
        button.titleLabel.lineBreakMode     = UILineBreakModeTailTruncation;
        button.contentVerticalAlignment     = UIControlContentVerticalAlignmentCenter;
        button.contentHorizontalAlignment   = UIControlContentHorizontalAlignmentCenter;
    
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitle:textFromField forState:UIControlStateNormal];
    
        UIImage *buttonBkground;
        buttonBkground = [UIImage imageNamed:@"blueButton.png"];
        UIImage *newImage = [buttonBkground stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
        [button setBackgroundImage:newImage forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];
    
    [self.tableView reloadData]
    }
    

    在动态创建按钮时,您必须注意单元格的宽度、高度等。

    【讨论】:

    • 您好,感谢您的回复。我对此有所了解,但没有出现任何东西。当我有机会时,我会再试一次,很抱歉我无法尽快回复(或投入足够的时间来解决我仍然遇到的问题)。我会尽快这样做。
    【解决方案3】:

    创建一个UIView 对象。将您的 UIButton 对象设置为此父视图的子视图。

    完成后,您可以将此父视图添加到单元格的 contentView 属性中。

    -tableView:cellForRowAtIndexPath: 委托方法中,您将根据某些条件状态生成父视图(以及内部按钮的数量和类型),然后将其添加到单元格的contentView

    至于以编程方式执行此操作,一旦某些条件发生变化,运行[tableView reloadData] 或类似方法来刷新表格视图及其单元格。

    【讨论】:

      【解决方案4】:

      您可以在 viewcontroller cellForRowAtIndexPath 函数中执行此操作:

      var btn = UIButton(frame: CGRectMake(100, 100, 100, 20));
          btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
          btn.setTitle("My Button", forState:UIControlState.Normal);
          btn.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside);
      
          //add the buttons to the cell
          cell.contentView.addSubview(btn)
      

      在同一个类中添加另一个函数,它将接收按钮点击事件

      func buttonTapped(sender: UIButton!)
      {
          println("Button Tapped")
      }
      

      【讨论】:

      • 当您向上或向下移动单元格时,上面的按钮会再次重新创建。
      • 在函数外创建按钮,检查按钮是否存在后添加到单元格中。如果有许多单元格,则维护一个地图或按钮索引。
      猜你喜欢
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多