gpt4 book ai didi

iphone - UItableViewGrouped中出现多次的文本字段

转载 作者:行者123 更新时间:2023-12-03 21:06:57 24 4
gpt4 key购买 nike

我在这里找到了一些如何在单元格中实现文本字段的示例代码: Having a UITextField in a UITableViewCell

但是,文本字段在表格的第一部分中多次出现在我的表格上,即使我指定它仅在出现到表格的第二部分和该部分的第一行时才出现。

有什么解释为什么会发生这种情况吗?我只想要它在第二部分第一行。但它似乎认为,每当第二部分出现时,它都会提前绘制文本字段。有没有办法将其“锁定”到特定的组和单元格?

- (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;
}

感谢所有做出贡献的人。

就功能而言,我还不太确定,但它离我想要的地方又近了一步:)。

最佳答案

尝试使用两个不同的单元格,一个带有文本字段,另一个没有。对两种不同类型的单元格使用不同的 CellIdentifier 字符串。这应该可以解决它。

关于iphone - UItableViewGrouped中出现多次的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6235014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com