简体   繁体   中英

How to achieve similar UI with UITextField inside UITableView (UITableViewCell)?

I am looking to mimic the following UI using a UITextField within a UITableViewCell (within a UITableView). I am new to MonoTouch and I can't seem to figure out what the code would look like for this.

在此输入图像描述

This is very simple. Just add a UITextField with no background color to the cell. Add the below code in your cellForRowAtIndexPath method.

UITextField *inputText = [[UITextField alloc]initWithFrame:CGRectMake(10,10,280,22)];
inputText.textAlignment = UITextAlignmentLeft;
inputText.backgroundColor = [UIColor clearColor];
inputText.placeHolder = @"Street";
[cell.contentView addSubview:inputText];
[inputText release];

The cell is a custom cell. It has some properties, a editable UITextField, and a placehold string for empty content. The following code is writed by hand, so maybe there are some bugs inside.

@interface EditableCell : UITableViewCell {
   UITextField *mTextField;
}
@property UITextField *textField;

- (void)setPlaceHoldString:(NSString *)placeHolder;
@end

@implement EditableCell
@synthesize textField = mTextField;

- (void)setPlaceHoldString:(NSString *)placeHolder
{
   self.textField.placeHolder = placeHolder;
}

- (UITextField *)textField
{
  if (mTextField == nil) {
      mTextField = [[UITextField alloc] init];

      // Configure this text field.
      ...

      [self addSubView:mTextField];
  }

   return mTextField;
}

- (void)dealloc
{
  self.textField = nil;
  [super dealloc];
}
@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM