如何在iOS中的UITableViewCells中显示UITextFields

时间:2014-01-30 12:40:55

标签: ios iphone objective-c

嗨我在UIView有一个按钮。我的要求是,如果我点击UITextField中显示的按钮UITableViewCells。我知道如果用户点击按钮,如何在UITextField中显示UIView s。但我不知道如果用户按下按钮,如何在UITextField s内显示UITableViewCell。请帮助我任何人。

 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

    {

    UITextField *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;

    [self.view addSubview:text1];


    UITextField *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;

    [self.view addSubview:text2];

    }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

    return 1;

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

    return 1;

    }

    - (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:@""];

    return cell;

    }

2 个答案:

答案 0 :(得分:1)

试试这个:

[yourSubview setTag:101];
[cell.contentView addSubview:yourSub];

in cellForRowAtIndexPath方法

您可以像这样恢复textField:

NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:ip]
UITextField *textField = (UITextField*)[cell viewWithTag:101];

注意101 - 这是你可以用来标记的号码。不同的视图(例如:cell.contentView)可能包含相同的标记。

答案 1 :(得分:0)

您需要子类UITableViewCell并为文本字段添加插座。如果你寻找它们,那里有很多例子。这是一个:

http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/

像这样(MyCustomTableViewCell.h):

#import <UIKit/UIKit.h>

@interface MyCustomTableViewCell : UITableViewCell
@property(nonatomic,weak) IBOutlet UITextField *myTextField;
@end

然后在cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell";
MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.myTextField.text = @"Whatever";

不要忘记#import "MyCustomTableViewCell.h"并在IB中连接该文本字段