在 UITableViewCell 中有一个 UITextField [英] Having a UITextField in a UITableViewCell
问题描述
我现在已经尝试这样做了几天,并且在阅读了大量尝试这样做的人的消息后,我仍然无法在我的一些 UITableViewCells
,就像在这个例子中:
I'm trying to do that for a couple of days now, and after reading tons of messages of people trying to do that too, I'm still unable to have a fully working UITextField
in some of my UITableViewCells
, just like in this example:
我的表单可以正常工作但文本不可见(尽管我将其颜色设置为蓝色),当我单击它时键盘会进入该字段并且我无法正确实现键盘事件.我尝试了一系列来自 Apple 的示例(主要是 UICatalog
,其中有一个类似的控件)但它仍然无法正常工作.
Either I have the form working but the text is not visible (although I set its color to blue), the keyboard goes on the field when I click on it and I haven't been able to correctly implement the keyboard events.
I tried with a bunch of examples from Apple (mainly UICatalog
, where there is a kinda similar control) but it's still not working correctly.
有人可以帮助我(以及所有试图实现此控件的人)并在 UITableViewCell
中发布一个 UITextField
的简单实现吗?>
Can somebody help me (and all the people trying to realize this control) and post a simple implementation of a UITextField
in a UITableViewCell
, that works fine?
推荐答案
试试这个.对我来说就像一个魅力(在 iPhone 设备上).我曾经将此代码用于登录屏幕.我将表格视图配置为有两个部分.你当然可以去掉部分条件.
Try this out. Works like a charm for me (on iPhone devices). I used this code for a login screen once. I configured the table view to have two sections. You can of course get rid of the section conditionals.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
if ([indexPath section] == 0) {
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if ([indexPath row] == 0) {
playerTextField.placeholder = @"example@gmail.com";
playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
playerTextField.returnKeyType = UIReturnKeyNext;
}
else {
playerTextField.placeholder = @"Required";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
playerTextField.textAlignment = UITextAlignmentLeft;
playerTextField.tag = 0;
//playerTextField.delegate = self;
playerTextField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
[playerTextField setEnabled: YES];
[cell.contentView addSubview:playerTextField];
[playerTextField release];
}
}
if ([indexPath section] == 0) { // Email & Password Section
if ([indexPath row] == 0) { // Email
cell.textLabel.text = @"Email";
}
else {
cell.textLabel.text = @"Password";
}
}
else { // Login button section
cell.textLabel.text = @"Log in";
}
return cell;
}
结果如下:
这篇关于在 UITableViewCell 中有一个 UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!