![](/img/trans.png)
[英]How can I take the text from UITextFields in UITableViewCells in a UITableView and put them into an array (Swift 3)?
[英]how can i display UITextfields inside of UITableviewcells if user click the “+” Button in UIView
我在UIView
有一个“ + ”按钮。 我的要求是,如果我单击UITableViewCells
显示的按钮UITextFields
。 我有一个想法,如果用户单击“ + ”按钮,如何在UIView
显示UITextFields
。 但是我不知道如果用户点击“ + ”按钮,如何在UITableViewCells
内部显示UITextFields
。 请任何人帮助我。 昨天我为这个功能而震惊。 我尝试了一些代码。 但是它不能正常工作。 但是我没有找到任何解决方案。 提前致谢。
UIButton *myGreenIconButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[myGreenIconButton1 addTarget:self action:@selector(GreenIconButtonClicked)forControlEvents:UIControlEventTouchUpInside];
[myGreenIconButton1 setBackgroundImage:[UIImage imageNamed:@"index.jpg"] forState:UIControlStateNormal];
myGreenIconButton1.backgroundColor = [UIColor clearColor];
myGreenIconButton1.frame = CGRectMake(285, 144, 25, 25);
[self.view addSubview:myGreenIconButton1];
-(void)GreenIconButtonClicked
{
view=[[UIView alloc]initWithFrame:CGRectMake(0, 80, 300, 20)];
text1=[[UITextField alloc]initWithFrame:CGRectMake(10, 80, 100, 20)];
text1.borderStyle=UITextBorderStyleRoundedRect;
text1.backgroundColor=[UIColor clearColor];
text1.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
text1.font=[UIFont systemFontOfSize:14.0];
text1.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
text1.textAlignment=NSTextAlignmentCenter;
text1.delegate=self;
[view addSubview:text1];
text2=[[UITextField alloc]initWithFrame:CGRectMake(120, 80, 100, 20)];
text2.borderStyle=UITextBorderStyleRoundedRect;
text2.backgroundColor=[UIColor clearColor];
text2.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
text2.font=[UIFont systemFontOfSize:14.0];
text2.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
text2.textAlignment=NSTextAlignmentCenter;
text2.delegate=self;
[view addSubview:text2];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.textLabel setText:@""];
[view setTag:101];
NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:ip];
UITextField *textField = (UITextField*)[cell1 viewWithTag:101];
UITextField *textField1=(UITextField*)[cell1 viewWithTag:101];
UITextField *textField2=(UITextField*)[cell1 viewWithTag:101];
[cell.contentView addSubview:text1];
[cell.contentView addSubview:text2];
[cell.contentView addSubview:text3];
return cell;
}
首先使用xib或不使用xib创建自定义单元,并按照以下方式为您工作:
CustomCell.h
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell {
}
@property (strong,nonatomic) IBOutlet UITextField *textField;
@end
CustomCell.m
#import "CustomCell.h"
@implementation CustomCell
@synthesize textField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.textField = [[UITextField alloc] init];
[self.contentView addSubview:self.textField];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
您的TableView .h
@property (strong,nonatomic) NSMutableArray *array;
@property (strong, nonatomic) IBOutlet UITableView *tblView;
您的表格视图.m
@synthesize array;
@synthesize tblView;
-(void)viewWillAppear:(BOOL)animated {
array = [[NSMutableArray alloc] init];
[tblView reloadData];
}
-(int)numberOfSectionsInTableView:(UITableView *)tableView
{
if( [array count ] > 0 )
{
return 1;
}
return 0;
}
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if( [array count ] > 0 )
{
return [array Count];
}
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:@"CustomCell%d%d",indexPath.row,indexPath.section];
[tblCreateProfile registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cellIdentifier =nil;
cell.textField.text = [array objectAtIndex:indexPath.row];
return cell;
}
-(void)GreenIconButtonClicked
{
[array addObject:@"Test"];
[tblView reloadData];
}
我为您的要求创建了一个示例,并将其发布在github上。 你可以在这里找到代码
我创建了一个带有文本字段的自定义表格视图单元格。
因此,只需尝试使用Custom TableViewCells。 这样您就可以实现此问题的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.