【发布时间】:2011-09-08 06:22:59
【问题描述】:
我在这里找到了一些如何在单元格中实现文本字段的示例代码: Having a UITextField in a UITableViewCell
但是,文本字段在表格的第一部分中多次出现在我的表格中,即使我指定它仅在表格的第二部分和该部分的第一行出现时。
任何解释为什么会发生这种情况?我只想要它在第 2 部分第 1 行。但似乎认为每当第二部分出现时,它只会提前绘制文本字段。有没有办法将它“锁定”到特定的组和单元格?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self CreateMultilinesCell:CellIdentifier];
}
NSLog(@"%d", [indexPath section]);
if ([indexPath section] == 1) {
NSLog(@"TextField");
if ([indexPath row] == 0 ) {
UITextField *replyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 185, 30)];
replyTextField.adjustsFontSizeToFitWidth = YES;
replyTextField.textColor = [UIColor blackColor];
replyTextField.keyboardType = UIKeyboardTypeDefault;
replyTextField.returnKeyType = UIReturnKeyDone;
replyTextField.backgroundColor = [UIColor grayColor];
replyTextField.autocorrectionType = UITextAutocorrectionTypeNo;
replyTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
replyTextField.textAlignment = UITextAlignmentLeft;
replyTextField.tag = 0;
replyTextField.delegate = self;
replyTextField.clearButtonMode = UITextFieldViewModeNever;
[replyTextField setEnabled: YES];
[cell.contentView addSubview:replyTextField];
[replyTextField release];
cell.detailTextLabel.text = @"something";
}
}
else {
cell.detailTextLabel.text = [separatedString objectAtIndex:indexPath.row];
}
return cell;
}
创建多行单元格:
- (UITableViewCell*) CreateMultilinesCell :(NSString*)cellIdentifier
{
//NSLog(@"Entering CreateMultilinesCell");
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.font = [self SubFont];
cell.detailTextLabel.textColor = [UIColor colorWithRed:10.0/255 green:10.0/255 blue:33.0/255 alpha:1.0];
[cell setBackgroundColor:[UIColor clearColor]];//]colorWithRed:.98 green:.98 blue:.99 alpha:1.0]];
[self.tableView setBackgroundColor:[UIColor clearColor]];//colorWithRed:.94 green:.96 blue:.99 alpha:1.0]];
//NSLog(@"Exiting CreateMultilinesCell");
return cell;
}
啊,我太低了,无法回答我自己的问题,所以我将在这里更新:
看起来像是让 2 个单元标识符工作。谢谢。刚学到新东西!哈哈。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSString *replyCellIdentifier = @"replyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *replyTextField;
if (cell == nil) {
if ([indexPath section] == 0) {
cell = [self CreateMultilinesCell:CellIdentifier];
}
else if ([indexPath section] == 1) {
NSLog(@"TextField");
cell = [self CreateMultilinesCell:replyCellIdentifier];
if ([indexPath row] == 0) {
replyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 185, 30)];
replyTextField.adjustsFontSizeToFitWidth = YES;
replyTextField.textColor = [UIColor blackColor];
replyTextField.keyboardType = UIKeyboardTypeDefault;
replyTextField.returnKeyType = UIReturnKeyDone;
replyTextField.backgroundColor = [UIColor grayColor];
replyTextField.autocorrectionType = UITextAutocorrectionTypeNo;
replyTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
replyTextField.textAlignment = UITextAlignmentLeft;
replyTextField.tag = 0;
replyTextField.delegate = self;
replyTextField.clearButtonMode = UITextFieldViewModeNever;
[replyTextField setEnabled: YES];
[cell.contentView addSubview:replyTextField];
[replyTextField release];
cell.detailTextLabel.text = @"something";
}
}
/*else {
cell.detailTextLabel.text = [separatedString objectAtIndex:indexPath.row];
}*/
}
NSLog(@"%d", [indexPath section]);
if ([indexPath section] == 0) {
cell.detailTextLabel.text = [separatedString objectAtIndex:indexPath.row];
}
return cell;
}
感谢所有做出贡献的人。
在功能方面我还不是很确定,但它离我想要的位置更近了一步:)。
【问题讨论】:
-
可能是因为你重复使用了单元格。
-
发布 [self CreateMultilinesCell:CellIdentifier] 中使用的代码,我认为问题出在那儿
标签: iphone uitextfield uitableview