0

I have a UITableView with this code below:

- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }


    if (indexPath.row == 0){
        cell.image.image = [UIImage imageNamed:@"male80.png"];
        cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Phone" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
    }

    if(indexPath.row == 1) {
        cell.image.image = [UIImage imageNamed:@"male80.png"];
        cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Mobile Phone" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
    }

    if (indexPath.row == 2) {
        cell.image.image = [UIImage imageNamed:@"gift41.png"];
        cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"E-mail" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
    }

    if (indexPath.row == 3) {
        cell.image.image = [UIImage imageNamed:@"gender.png"];
        cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"address" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
    }

    if (indexPath.row == 4) {
        cell.image.image = [UIImage imageNamed:@"gender.png"];
        cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"country" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
    }

    if (indexPath.row == 5) {
        cell.image.image = [UIImage imageNamed:@"gender.png"];
        cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"city" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
    }

    if (indexPath.row == 6) {
        cell.image.hidden = YES;
        cell.text.hidden = YES;

        UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, cell.frame.size.width - 18, cell.frame.size.height - 15)];
        view.text = @"Text Example";
        view.textColor = [UIColor whiteColor];
        view.backgroundColor = [UIColor clearColor];
        view.tag = 13;
        view.layer.borderWidth = 0.5f;
        view.layer.cornerRadius = 4;
        view.layer.borderColor = [[UIColor grayColor] CGColor];
        [cell addSubview:view];
    }



    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row != 6) {
        return 65;
    }else{
        return 204;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 7;
}

This code is very simple, the problem with him is:

  • When I run my app first time, I can see my fields organized in the manner prescribed (Like I put in cellforRowAtIndexPath)
    • When I scroll down (put my fields out of window) and scroll to up again I see a problem!

The problem is that my table, create a UITextview in rows like 0, 1 and 2. But why this is happening? in my code I made it clear! That only the row 6 will be created a UITextView!

How can I solve this problem?

LettersBa
  • 747
  • 1
  • 8
  • 27

4 Answers4

1

Two issues:

1) your cell is recycled, this means when the cell gets to row 6 your UITextView is added to the cell and then when scrolling back the UITextView is still there

2) and you should add the subviews of cell in its contentView and not the cell itself.

Solution :

Use tow kind of UITableViewCell, one specific for row 6 and another one for the other rows. Register the cells and then dequeue them for the appropriate indexPath.

netbe
  • 236
  • 2
  • 8
0

I think your problem is loading your NIB file into the table view. Look into Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

I tried to recreate your problem by creating a UITableViewCell using Interface Builder called ItemCell and it works. Make sure you have this in your viewDidLoad

[super viewDidLoad];

//Load the NIB file
UINib *nib = [UINib nibWithNibName:@"ItemCell"
                            bundle:nil];

//Register this NIB, which contains the cell
[self.tableView registerNib:nib
     forCellReuseIdentifier:@"ItemCell"];

Then replace

   static NSString *CellIdentifier = @"MyCell";
   TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

with

    ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell"
                                                 forIndexPath:indexPath];

if (cell == nil) {
    cell = [[ItemCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ItemCell"];
}

You might want to also look into putting your images in a data structure to go with a MVC design pattern.

Community
  • 1
  • 1
Mochi
  • 1,059
  • 11
  • 26
-2

Give table Row 6 a different identifier than the others

static NSString *CellIdentifier = @"MyCell";
if (indexPath.row == 6){
   CellIdentifier = @"MyCell6";
}

Even then you shouldn't be re allocing the textField everytime cellForRowAtIndexPath is called. That's the point of reusing the cell! But that's another conversation so look up how to properly recycle cells

MobileMon
  • 8,341
  • 5
  • 56
  • 75
  • Error, ˜'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier MyCell6 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'˜ – LettersBa Nov 04 '15 at 18:32
  • Well, I need to create another cell in my table using the interface and give him a identifier... – LettersBa Nov 04 '15 at 18:33
-2

Put this code in viewDidLoad and make you view a global variable.

    UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, cell.frame.size.width - 18, cell.frame.size.height - 15)];
    view.text = @"Text Example";
    view.textColor = [UIColor whiteColor];
    view.backgroundColor = [UIColor clearColor];
    view.tag = 13;
    view.layer.borderWidth = 0.5f;
    view.layer.cornerRadius = 4;
    view.layer.borderColor = [[UIColor grayColor] CGColor];

And now create the tableView like this:

 - (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      static NSString *CellIdentifier = @"MyCell";
      TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell == nil) {
    cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
view.hidden = YES;

if (indexPath.row == 0){
    cell.image.image = [UIImage imageNamed:@"male80.png"];
    cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Phone" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}

if(indexPath.row == 1) {
    cell.image.image = [UIImage imageNamed:@"male80.png"];
    cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Mobile Phone" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}

if (indexPath.row == 2) {
    cell.image.image = [UIImage imageNamed:@"gift41.png"];
    cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"E-mail" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}

if (indexPath.row == 3) {
    cell.image.image = [UIImage imageNamed:@"gender.png"];
    cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"address" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}

if (indexPath.row == 4) {
    cell.image.image = [UIImage imageNamed:@"gender.png"];
    cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"country" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}

if (indexPath.row == 5) {
    cell.image.image = [UIImage imageNamed:@"gender.png"];
    cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"city" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}

if (indexPath.row == 6) {
    cell.image.hidden = YES;
    cell.text.hidden = YES;
    view.hidden = NO;
    [cell addSubview:view];
}



return cell;

}

Bhavuk Jain
  • 2,167
  • 1
  • 15
  • 23
  • No, it has the same issue. You blindly attempt to add a text view subview over and over to the cell at row 6 (leading to multiple text view in the cell) and you make no attempt to ensure the text view is removed when the cell is reused for other rows. And creating a single text view in `viewDidLoad` is a hack. What if the OP changes the requirement and ends up needing a text view in two rows later on? – rmaddy Nov 04 '15 at 19:02
  • Agreed with your comment. But I've answered this in respect of the question and what the user wants at the moment. And you can also mention if cell contains that textview, remove it from the cell. But still I think, it's fine and it will not create any memory issues as well. – Bhavuk Jain Nov 04 '15 at 19:09
  • @LettersBa I think you haven't created the textView properly. You need to pass the width and height. – Bhavuk Jain Nov 04 '15 at 19:12
  • Yes.. I using: UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, 200, 200)]; and not working, I only see back screen... – LettersBa Nov 04 '15 at 19:55