2014-01-30 4 views
-2

Привет, у меня есть одна кнопка в UIView. Мое требование - если я нажму эту кнопку UITextField s отображается в UITableViewCells. У меня есть идея, как отображать UITextField s в UIView, если пользователь нажимает кнопку. Но я не знаю, как отображать UITextField s внутри UITableViewCell s, если пользователь нажимает кнопку. Пожалуйста, помогите мне никому.Как отображать UITextFields внутри UITableViewCells в iOS

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; 

    } 
+0

Будьте конкретны для того, чего вы хотите достичь. Сначала сделайте все основные понятия понятными. Сначала прочтите UITableView Controller, классы UITableViewCell и их ссылки из apple docs. Удачи. – iShwar

ответ

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

1

Попробуйте использовать это:

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

в методе cellForRowAtIndexPath

И вы можете восстановить свой TextField, как это:

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

Обратите внимание на 101 - это число, которое вы можете использовать для тега. И разные представления (например: cell.contentView) могут содержать одинаковые теги.

Смежные вопросы