ios 按下按钮时添加按钮

标签 ios uibutton subclass

首先,我是 ios 开发的新手,我想了解如何解决这个问题。

此屏幕截图来自另一个应用程序。当您按下绿色加号按钮时,它会根据您按下绿色加号按钮的次数添加更多按钮,例如顶部的按钮

enter image description here

我正在尝试复制这个但不知道从哪里开始。我的问题是,最好为顶部按钮创建一个子类吗?然后在每次按下绿色加号按钮时以某种方式附加该子类按钮?

最佳答案

我想自己学习,所以我创建了示例项目,这里是如何完成此操作的简单教程。并不完美,但您可能已经了解其工作原理的基本概念。


在 Interface Builder 中创建 TableView Controller 。然后将按钮和文本字段添加到原型(prototype)单元格。

Table view controller and prototype cells


向您的项目添加新类,我们将其命名为 MyTableViewController。将 TableView Controller 的类设置为 MyTableViewController(在界面构建器中)。

enter image description here


设置原型(prototype)单元格的标识符。我使用了 defaultCell 值。

enter image description here


为按钮和文本字段设置标签值。

enter image description here

我使用了这些标签值:

  • 加号按钮:100
  • 时间按钮:110
  • 项目按钮:120
  • 文本字段 1:10
  • 文本字段 2:20
  • 文本字段 3:30

MyTableViewController.h(注意: UITableView 链接为 tView 变量)

#import <UIKit/UIKit.h>

@interface MyTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>

/* Data array */
@property (nonatomic) NSMutableArray *data;
/* Expanded cell */
@property (nonatomic) NSInteger expandedCell;

/* Table view */
@property (strong, nonatomic) IBOutlet UITableView *tView;

@end 

MyTableViewController.m

#import "MyTableViewController.h"

@interface MyTableViewController ()

@end

@implementation MyTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    /* No cells expanded by default */
    _expandedCell = -1;

    /* Create data array */
    _data = [NSMutableArray new];

    /* Add two cells to data array */
    UITableViewCell *cell = [self newDefaultCell:0];
    [_data addObject:cell];
    UITableViewCell *cell2 = [self newDefaultCell:1];
    [_data addObject:cell2];
}

- (UITableViewCell *)newDefaultCell:(NSUInteger)index {

    /* Initialize new UITableViewCell using prototype cell */
    UITableViewCell *cell = [_tView dequeueReusableCellWithIdentifier:@"defaultCell"];

    /* Initialize buttons for this cell */
    UIButton *plusButton = (UIButton *)[cell viewWithTag:100];
    [plusButton setTag:index];
    [plusButton addTarget:self action:@selector(plusButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *timeButton = (UIButton *)[cell viewWithTag:110];
    [timeButton setTag:index];
    [timeButton addTarget:self action:@selector(timeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *itemButton = (UIButton *)[cell viewWithTag:120];
    [itemButton setTag:index];
    [itemButton addTarget:self action:@selector(itemButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* Returns UITableViewCell object from data array */
    return [_data objectAtIndex:indexPath.row];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    /* If cell is expanded, return row height 140 */
    if(_expandedCell == indexPath.row) {
        return 140;
    } /* else return row height 30 */
    else return 30;
}

- (void)plusButtonPressed:(id)sender {
    /* Create new UITableViewCell */
    UITableViewCell *newCell = [self newDefaultCell:[_data count]];
    /* Add UITableViewCell to data array */
    [_data addObject:newCell];

    /* Update table view */
    [_tView beginUpdates];
    [_tView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:([_data count]-1)inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
    [_tView endUpdates];
}

- (void)timeButtonPressed:(id)sender {
    UIButton *button = (UIButton*)sender;

    /* Expand this cell to show UITextFields */
    _expandedCell = [button tag];

    /* UITableViewCell from data array, index is button's tag value */
    UITableViewCell *cell = [_data objectAtIndex:[button tag]];

    /* You can access text fields using viewWithTag: call */
    UITextField *tf1 = (UITextField *)[cell viewWithTag:10];
    UITextField *tf2 = (UITextField *)[cell viewWithTag:20];
    UITextField *tf3 = (UITextField *)[cell viewWithTag:30];

    /* Reload UITableViewRow */
    NSArray *indexes = @[[NSIndexPath indexPathForRow:[button tag] inSection:0]];
    [_tView reloadRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationTop];
    [_tView beginUpdates];
    [_tView endUpdates];
}

- (void)itemButtonPressed:(id)sender {

}

@end

完成了。

关于ios 按下按钮时添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19965615/

相关文章:

ios - 如何为静态表格 View 单元格设置自动布局?

iphone - UIButton 优于 UIImageView(优于 UIScrollView)

java - 实例化抽象父类(super class)的子类

iOS 从导航 Controller 子类设置工具栏项

Scala:是否可以定义一个扩展其类参数的类?

ios - 重复符号链接(symbolic link)器错误

ios - 如何使用 UITableView

ios - 将 UIButton 的触摸限制在指定区域

ios - UIButton 上需要单击和双击事件吗?

ios - 无法在 NSNotificaitonCenter 的 addObserver : selector: name: object: method 中使用 UIApplicationDidEnterBackgroundNotification 作为名称