按下按钮时的ios追加按钮
首先,我对ios开发还不熟悉,我正试图了解如何实现这一点 此屏幕抓图来自另一个应用程序。当您按下绿色加号按钮时,它会根据您按下绿色加号按钮的次数附加更多类似顶部的按钮按下按钮时的ios追加按钮,ios,uibutton,subclass,Ios,Uibutton,Subclass,首先,我对ios开发还不熟悉,我正试图了解如何实现这一点 此屏幕抓图来自另一个应用程序。当您按下绿色加号按钮时,它会根据您按下绿色加号按钮的次数附加更多类似顶部的按钮 我试图复制这一点,但不知道从哪里开始。我的问题是,最好为top按钮创建一个子类吗?然后,每次按下绿色加号按钮时,都会以某种方式附加该子类按钮?我想自己学习这个,所以我创建了一个示例项目,下面是如何实现这一点的简单教程。虽然不完美,但也许你已经了解了它的基本原理 在Interface Builder中创建表视图控制器。然后将按钮
我试图复制这一点,但不知道从哪里开始。我的问题是,最好为top按钮创建一个子类吗?然后,每次按下绿色加号按钮时,都会以某种方式附加该子类按钮?我想自己学习这个,所以我创建了一个示例项目,下面是如何实现这一点的简单教程。虽然不完美,但也许你已经了解了它的基本原理
在Interface Builder中创建表视图控制器。然后将按钮和文本字段添加到原型单元
将新类添加到项目中,我们将其命名为MyTableViewController。将表视图控制器的类设置为MyTableViewController(在interface builder中)
为原型单元设置标识符。我使用了
defaultCell
value
设置按钮和文本字段的标记值 我使用了这些标记值:
- 加号按钮:100
- 时间按钮:110
- 项目按钮:120
- 文本字段1:10
- 文本字段2:20
- 文本字段3:30
MyTableViewController.h(注意:UITableView链接为
tView
变量)
完成了。考虑使用
UITableView
进行此操作,并在每次按下按钮时添加一个新单元格。我尝试使用UITableView,但无法使其按我所需的方式工作,因为当您按下顶部按钮上的时间或项目时,它会变成这样的UITableView
#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
#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