代码之家  ›  专栏  ›  技术社区  ›  Muju

滚动时,自定义tableviewcell中的Textfields退出单元格

  •  0
  • Muju  · 技术社区  · 7 年前

    我的问题是,我通过xib将textfield放置在tableviewcell上。我正在我的viewcontroller类中访问这个自定义单元格,该类具有tableview。下面是我的代码 cellForRowAtIndexPath:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *STI=@"STII";
        AnalysedScreenCell *cell = (AnalysedScreenCell *)[tableView dequeueReusableCellWithIdentifier:STI];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AnalysedScreenCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.accessoryType=UITableViewCellAccessoryNone;
        }
    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        cell.txtDetails.delegate=self;
        cell.txtDetails.text = [countArray objectAtIndex:indexPath.row];
        [cell.txtDetails setTag:1];
        [cell.txtDetails addTarget:self action:@selector(textFieldDidChange:) forControlEvents: UIControlEventEditingChanged];
    
        cell.txtName.delegate=self;
        cell.txtName.text = [nameArray objectAtIndex:indexPath.row];
        [cell.txtName setTag:2];
        [cell.txtName addTarget:self action:@selector(textFieldDidChange:) forControlEvents: UIControlEventEditingChanged];
    
        cell.txtDate.delegate=self;
        cell.txtDate.text = [dateArray objectAtIndex:indexPath.row];
        [cell.txtDate setTag:3];
        [cell.txtDate addTarget:self action:@selector(textFieldDidChange:) forControlEvents: UIControlEventEditingChanged];
    return cell;
    }
    

    但是当我尝试滚动tableview时,table view中的textfields会进入另一个单元格。我还试图为它设置一个可重用的标识符,但它不起作用。

    这是我问题的图像

    Tableview when scrolled

    3 回复  |  直到 6 年前
        1
  •  2
  •   KKRocks    7 年前

    在类中添加此委托方法>

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 100; // change as per your needs
    
    }
    
        2
  •  0
  •   Burnie777    7 年前
        3
  •  0
  •   halfer    6 年前

    问题可能是

    因此,请提供表视图单元格的高度,使其包含文本字段的高度。

    例如:

    假设您的文本字段高度为40,您将上边距和下边距分别设置为10和10。

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 60; 
    }