【问题标题】:textField appears twice in UITableViewtextField 在 UITableView 中出现两次
【发布时间】:2012-08-08 02:52:37
【问题描述】:

我的TableViewController 中有ageTextField 属性。我将它添加到第一部分单元格的 subciew 中,但我不知道为什么当我向下滚动时它会显示在另一部分 (#3) 中..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    switch (indexPath.section){
        case 0:
        {
            [cell addSubview:self.ageTextField];
        }
        break;
        case 1:
        {
            cell.textLabel.text = [screeningArray objectAtIndex:indexPath.row];
            if (indexPath.row == 0)     //no choice
                cell.accessoryType = self.doneScreening ? UITableViewCellAccessoryNone:UITableViewCellAccessoryCheckmark;
            else                        //yes choice
                cell.accessoryType = self.doneScreening ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
        }
        break;
        case 2:
        {
            cell.textLabel.text = @"1 : ";
            [cell addSubview:self.riskTextField];
        }
        break;
        case 3:
        {
            cell.textLabel.text = [findingsArray objectAtIndex:indexPath.row];
            cell.accessoryType = [[boolValuesArray objectAtIndex:indexPath.row] boolValue] ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
        }
        break;
        default:
        break;
    }
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    if (textField == self.ageTextField)
        return (newLength > 2) ? NO : YES;
    else
        return (newLength > 7) ? NO : YES;
}

这是开头的截图..

http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.26.47%20PM.png

向下滚动后,注意ageTextField 的占位符是如何在Section3,cell3..中复制的。

http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.28%20PM.png

再次向上滚动时,section3中cell3的文本出现在第一个单元格中..

dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.52%20PM.png

这很奇怪,我不知道发生了什么!请帮忙..

【问题讨论】:

    标签: uitableview uitextfield


    【解决方案1】:

    如果重复使用表格视图单元格的内容(即,如果单元格!= nil),则必须删除它们。此外,应将子视图添加到表格单元格的 contentView 而不是单元格本身,因此子视图在进入/退出编辑模式时会适当调整。

            [cell.contentView addSubview:self.ageTextField];
    
            [cell.contentView addSubview:self.riskTextField];
    

    以下代码为重复使用准备单元格。

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    else
    {
        // Prepare cell for reuse
    
        // Remove subviews from cell's contentView
        for (UIView *view in cell.contentView.subviews)
        {
            // Remove only the appropriate views
            if ([view isKindOfClass:[UITextField class]])
            {
                [view removeFromSuperview];
            }
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.text = nil;
    }
    

    【讨论】:

    • 我修复了答案中的一个错误并提供了更多信息。请适当地更新您的代码。
    • @0x141E 我不小心对这个答案投了反对票。您是否可以进行任何可能的修改,以便我可以撤回此投票并将其恢复为赞成投票?
    【解决方案2】:

    当表格给你一个可重用的单元格时,它并不关心它以前在哪个部分中使用过。由于你添加了一个子视图,无论它是一个新单元格还是以前添加了一个子视图的单元格,很有可能得到更多一种和多种。

    【讨论】:

    • 我使用了传递参数 indexPath 中的 section 属性来控制应该考虑哪个单元格。有没有更好的办法?
    • 对每个部分使用不同的标识符,并且仅在新单元格时添加额外的视图。
    猜你喜欢
    • 2019-07-16
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多