0

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Muju
  • 884
  • 20
  • 54

3 Answers3

2

Add this delegate method in your class >

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

}
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

The issue might be

You have set the height of table view cell smaller so that the textField height does not fit into it.

So provide your height of table view cell so that it will contain the textField's height.

For example :

Lets say your textField height is 40, you left top and bottom margin as 10 and 10 respectively.

So provide the height as 60 or more than 60.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60; 
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
0

What @KKRocks said works perfectly...

The other option is to set in in the properties of the tableview as such:

enter image description here

Burnie777
  • 395
  • 5
  • 18