iosobjective-cuitableviewscrollreuseidentifier

Textfields in custom tableviewcell goes out of cell while scrolling


I am working on an application where i am using a UITableview with custom tableviewcell on XIB.

My problem is, I have placed textfield on tableviewcell through xib. I am accesing this custom cell in my viewcontroller class which is having a tableview. Here below is my code for cellForRowAtIndexPath: method.

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

But when I try to scroll the tableview, the textfields in the table view goes into another cell. I also tried to set a reuseable identifier to it, but its not working.

Here is the image of my problem

Tableview when scrolled


Solution

  • Add this delegate method in your class >

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 100; // change as per your needs
    
    }