【问题标题】:Textfield in appearing multiple times UItableViewGrouped文本字段多次出现 UItableView 分组
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 是的。在这种情况下,使用两个不同的单元格比添加额外的代码/负担从 cell.contentView 中删除文本字段并在特定单元格中再次添加它要好,只要该单元格被重新使用。
  • 不同的问题(单元格在滚动过程中出现多次),使用 2 个不同的单元格标识符解决。谢谢!
【解决方案2】:

那是因为你的这部分代码在 if(cell == nil) 之外

if ([indexPath row] == 0 ) {
    UITextField *replyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 185, 30)];
    ...........

}

所以这行代码总是在你滚动表格视图以查看该行并因此每次添加 uitextfield 时都会被调用,所以你可以做什么

在此块中声明该变量if(cell == nil){....}

此外,如果您没有在表格视图中添加或删除任何行或部分,那么它也可以正常工作。

*PS.*当您使用 tableview 来显示用户可以编辑的动态数据时,处理起来要困难得多。

你将不得不做更多的事情,但现在我认为你可以开始了。

【讨论】:

  • 感谢 robin,我现在正在处理它,一旦我开始工作就会更新。
【解决方案3】:

如果你使用的部分试试这个

NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]

如果您不使用部分,请使用

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i",indexPath.row];

【讨论】:

  • 这将为表格中的每一行创建单独的单元格,这可能会增加内存并在滚动表格视图时可能会变慢
猜你喜欢
  • 2020-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多