UITableView 单元格内的 UITextField
Posted
技术标签:
【中文标题】UITableView 单元格内的 UITextField【英文标题】:UITextField inside UITableView cell 【发布时间】:2010-10-27 05:17:39 【问题描述】:我一直在阅读很多东西,试图找出我做错了什么。我一直在尝试在 UITableView 单元格中添加 4 个 UITextFields。我有通过 IB 创建的 UITextFields 以及 UITableView。我将文本字段添加到 NSMutableArray 中,然后在 cellForRowAtIndexPath 中将这些文本字段对象添加到数组中。分成4个细胞。但是,这就是我的问题出现的地方。表格中的文本字段未排列(img:http://cl.ly/5a02f3acdd44d8a08125)。这是一些代码:
viewDidLoad:
textBoxList = [[NSMutableArray alloc] init];
[textBoxList addObject: txtName];
[textBoxList addObject: txtIP];
[textBoxList addObject: txtPort];
[textBoxList addObject: txtPassword];
cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] init] autorelease];
// Set up the cell...
[cell.contentView addSubview: [textBoxList objectAtIndex:[indexPath row]]];
return cell;
这是因为我在 IB 中设置了这些 UITextField 而不是动态创建它们吗?我的代表有什么事吗?我迷路了……
【问题讨论】:
【参考方案1】:您需要设置标签相对于单元格的框架。
【讨论】:
喜欢这个? txtName = [[UITextField alloc] initWithFrame:CGRectMake(0,0,100,30)]; txtIP = [[UITextField alloc] initWithFrame:CGRectMake(0,0,100,30)]; txtPassword = [[UITextField alloc] initWithFrame:CGRectMake(0,0,100,30)]; txtPort = [[UITextField alloc] initWithFrame:CGRectMake(0,0,100,30)]; 好吧,如果您已经在加载的笔尖中添加了所有新标签,那么您不想创建所有新标签。除非你放弃这种策略? 我仍在使用该策略。不知道如何在它们上设置框架 好吧,我不知道你把它们放在笔尖里(而不是仅仅把字符串放在 .strings 文件中)真的给自己买了多少。但是,撇开这些不谈,'frame' 是 UIView 的一个属性。所以 txtName.frame = CGRectMake(...)以上是关于UITableView 单元格内的 UITextField的主要内容,如果未能解决你的问题,请参考以下文章
UITableView“dequeueReusableCellWithIdentifier”导致基于单元格内容的动态单元格高度的单元格重叠?