UITextView content disaapearing

Discussion in 'iPhone/iPad Programming' started by cmezak, Jul 23, 2008.

  1. macrumors member

    #1
    Hey all,

    Haven't had any luck etting assistance in this forum yet, but I'll keep trying.

    So I have a tableView that contains a cell that contains a UITextView as a subview. When I scroll so that this cell leaves the screen, and then scroll back, the textView's content is gone, aside from a few pixels on the left side of it. Here's my code to put the textView in the cell. See anything amiss?

    Code:
    textFrame = cell.contentView.frame;
    textFrame.origin.x += 5;
    textFrame.origin.y += 10;
    textFrame.size.width = 250;
    textFrame.size.height = 180;
    UITextView *textView = [[UITextView alloc] initWithFrame:textFrame];
    cellText = [[detailBird description] mutableCopy];
    textView.text = cellText;
    textView.editable = NO;
    textView.userInteractionEnabled = NO;
    [cell.contentView addSubview:textView];
    
    Thanks!

    Charlie
     
  2. macrumors member

    #2
    Does anybody have any insight on this one? I still haven't been able to sort it out.

    I've tried allocating and initializing the textView elsewhere in the code. Nothing seems to work. This is especially confusing to me because the same code seems to be working fine with instances of other UIViews like UILabel.

    Whats the deal?

    chuck
     
  3. Moderator emeritus

    kainjow

    #3
    So you're saying that exact code works if you use a different view?

    You should initialize your view with CGRectZero, and override layoutSubviews (calling super first) and set its frame there based on contentView.bounds.

    Edit: your code is leaking textView and cellText.
     
  4. macrumors member

    #4
    Thanks for the reply.

    The text in the cell shows up when I select the cell, but does not display when the cell is not selected . . . whats the deal?

    I will post my entire code for cellForRowAtIndexPath here:

    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    	
    	UIImage *image;
    	UILabel *labelView;
    	UILabel *dataView;
    	UITextView *textView;
    	NSMutableString *cellText = nil;
    	switch (indexPath.section) {
    		case 0:
    			image = [UIImage imageNamed:[[detailBird scientificName] stringByAppendingString:@".jpg"]];	
    			cell.image = image;
    			//[image release];
    			break;
    		case 1:
    			switch (indexPath.row) {
    				case 0: //cell contains scientific name
    					//print label that says "scientific name: "
    					labelView = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 280, 20)];
    					labelView.text = @"Scientific Name:";
    					labelView.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
    					[cell.contentView addSubview:labelView];
    					[labelView release];
    					
    					//print object's scientific name
    					dataView = [[UILabel alloc] initWithFrame:CGRectMake(120, 5, 170, 20)];
    					dataView.text = [detailBird scientificName];
    					dataView.adjustsFontSizeToFitWidth = YES;
    					dataView.font = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
    					[cell.contentView addSubview:dataView];
    					[dataView release];
    					break;
    				case 1: 
    					//cell contains Family
    					//print label that says "family:"
    					labelView = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 280, 20)];
    					labelView.text = @"Family:";
    					labelView.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
    					[cell.contentView addSubview:labelView];
    					[labelView release];
    					
    					//print object's family name
    					dataView = [[UILabel alloc] initWithFrame:CGRectMake(120, 5, 170, 20)];
    					dataView.text = [detailBird family];
    					dataView.adjustsFontSizeToFitWidth = YES;
    					dataView.font = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
    					[cell.contentView addSubview:dataView];
    					[dataView release];
    					break;
    				case 2: 
    					//cell contains height
    					//print label that says "size:"
    					labelView = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 280, 20)];
    					labelView.text = @"Size:";
    					labelView.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
    					[cell.contentView addSubview:labelView];
    					[labelView release];
    					
    					//print object's size
    					dataView = [[UILabel alloc] initWithFrame:CGRectMake(120, 5, 120, 20)];
    					cellText = [[detailBird size] mutableCopy];
    					cellText = [[cellText stringByAppendingString:@"\""] mutableCopy];
    					dataView.text = cellText;
    					dataView.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
    					[cell.contentView addSubview:dataView];
    					[dataView release];
    					break;
    				default:
    					break;
    			}
    			break;
    		case 2:
    			//cell contains description
    			textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 200, 200)];
    			textView.text = [detailBird description];
    			NSLog(textView.text);
    			textView.editable = NO;
    			textView.userInteractionEnabled = NO;
    			[cell.contentView addSubview:textView];
    			//[textView release];
    			break;
    		default:
    			break;
    	}
    	[cellText release];
        return cell;
    }
    I think the problem may actually be in my switch syntax . . . see anything?
     
  5. Moderator emeritus

    kainjow

    #5
    If you're customizing a table cell beyond simple text color/font/etc, you need to create a subclass. Apple has several examples on this on their iPhone Dev Center website.
     
  6. macrumors member

    #6
    Reuse Queue?

    I suspect that the problem lies with your use of the reuse queue. You seem to have three "styles" of UITableViewCell, one in each section, yet you are only using a single reuse identifier for all cells in your table view. So there's a good chance that when a new cell is scrolled onto the screen, the cell that is dequeued and displayed will be the wrong type.

    To test this, I suggest that you duplicate the
    Code:
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    block, and add it inside the case blocks for each section, using a different identifier in each case. Then, you should find that the relevant type of cell gets dequeued when the table is scrolled.
     

Share This Page