如果用户单击UIView中的“+”按钮,如何在UITableviewcells内显示UITextfields

时间:2014-01-31 05:27:28

标签: ios iphone objective-c

我在UIView中有一个“ + ”按钮。我的要求是,如果我点击UITextFields中显示的按钮UITableViewCells。如果用户点击“ + ”按钮,我知道如何在UITextFields中显示UIView。但如果用户点击“ + ”按钮,我不知道如何在UITextFields内显示UITableViewCells。请帮助我任何人。昨天我发现了这个功能。我尝试了一些代码。但它没有正常工作。但我没有找到任何解决方案。提前谢谢。

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;

}

3 个答案:

答案 0 :(得分:1)

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


}

答案 1 :(得分:1)

我已根据您的要求创建了一个示例,并将其发布在github上。您可以找到代码Here

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

答案 2 :(得分:0)

所以只需尝试自定义TableViewCells。这样您就可以实现此问题的解决方案。