views:

350

answers:

2

Hi guys I'm working in cellForRowAtIndexPath, and I'm trying to embed a textview into the cell. Now, the embedding part is all well and fine, but I am getting the strangest problem when I try and load the textview in all but the first row.

Note: I want The TextView in all but the first row in my table.

The problem is that when I implement an if statement to check for indexpath.row and then scroll down the text view is then NOT visible in some cells. It is somehow related to me having cells with quite large heights >200 and they are initially off screen. When I scroll down it is like the next cell underneath the bottom most visible cell is now responding to a indexpath row of ==0.

I am completely baffled, has anyone any ideas?

My Code

// Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
   CGRect frame; frame.origin.x = 5; frame.origin.y = 10; frame.size.width = 20; frame.size.height = 25; 

           // ..some unrelated setup in here

  //textbox setup
  textView = [[UITextView alloc] initWithFrame:CGRectMake(30, 80, 500, 150)];
  textView.layer.cornerRadius = 10;
  textView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.2];
  textView.font = [UIFont fontWithName:@"Helvetica" size:17];
  textView.text = @"test-content";
  textView.layer.borderWidth = 1;
  textView.layer.borderColor = [[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.9] CGColor];
  textView.tag = 4;

  if (indexPath.row != 0) {
  [cell.contentView addSubview:textView];
  [textView release];  
  }
   }
  // a little more unrelated stuff


return cell;

}

Can someone see the problem?

Thanks

+1  A: 

Why are you setting up the textView even when indexPath.row is not zero (and you don't release if indexPath.row is zero)?

Try moving the creation and setup to inside the "if (indePath.row != 0)" block (just above the addSubView line).

DyingCactus
This I had previously tried, and something was still a little weird. A fix occured when i changed the if() to a switch/case function.
norskben
A: 

Your problem may be that when you create the cells, you give them all the same cell identifier. When the first cell is queued, it does not have a text view, yet this part of your code is called:

if (cell == nil)

A cell is returned and isn't nil, but in the case of the first cell, it doesn't have a text view. Try changing the cell identifier for the first cell to see if you have better results; this will prevent it from being re-used.

Jeff Kelley
Isn't that what I suggested in the code below?
Felix
Your code creates a text view for each cell and hides it if the cell is at row 0. That results in an unnecessary text view being created. You suggest it, but then do the opposite.
Jeff Kelley
I have update the code.
Felix
@Felix, your code looks good, but the comments still misinterpret what's going on. I suggest you read the Table View Programming Guide - your concept of creating one "master cell" is incorrect. "cell == nil" will be true more than once per cell identifier.
Jeff Kelley
I did indeed misunderstand this, i checked it again. The documentation wasn't very clear here. I will delete my post.
Felix