如果用户在UIView中单击"+"按钮,如何在UITableview单元格内显示UITextfields。



我有一个" " 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;
}

我为您的需求创建了一个示例,并将其发布在github上。您可以在这里找到代码

我创建了一个带有文本字段的自定义表视图单元格。

首先使用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];

}

因此,只需在自定义tableviewcells中尝试即可。这样您就可以解决此问题的解决方案。

最新更新