问题描述
|
我在这里找到了一些如何将文本字段实现为单元格的示例代码:
在UITableViewCell中有一个UITextField
但是,即使我指定仅在出现在表的第二部分和该部分的第一行时,该文本字段才会在我的表的表的第一部分中多次出现。
为什么会发生这种情况的任何解释?我只想要第二部分第一行。但是似乎认为每当第二部分出现时,它都会提前绘制文本字段。是否可以将其“锁定”到特定的组和单元?
- (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,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;
}
createmultilinecell:
- (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 cellidentifiers工作。谢谢。刚学到新东西!哈哈。
- (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,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;
}
感谢所有贡献的人。
在功能方面,我还不太确定,但是离我想要的位置还很近:)。
解决方法
尝试使用两个不同的单元格,一个单元格带有textField,另一个单元格不带。对两种不同类型的单元格使用不同的“ 3”字符串。那应该解决它。
, 那是因为您在ѭ4part之外拥有这部分代码
if ([indexPath row] == 0 ) {
UITextField *replyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0,185,30)];
...........
}
因此,当您滚动表格视图以查看该行并每次添加uitextfield时,总是会调用此代码行,因此您可以执行此操作
在此块ѭ6中声明该变量
另外,如果您没有在表视图中添加或删除任何行或节,则将可以正常工作。
* PS。*使用tableview显示用户可以编辑的动态数据时,它很难处理。
您还需要做更多的事情,但就目前而言,我认为您很好上手。
, 如果您使用的部分尝试一下
NSString *CellIdentifier = [NSString stringWithFormat:@\"S%1dR%1d\",indexPath.section,indexPath.row]
如果您不使用部分,则使用
NSString *CellIdentifier = [NSString stringWithFormat:@\"Cell%i\",indexPath.row]
;