Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some UITableView cells disappear after scrolling down and then back

I have four UITableViews side-by-side, created programmatically. Problem 1: The 1st cell is always blank. Problem 2: When I scroll down, some cells are blank. When I scroll back and forth, other cells disappear.

Here is the code where the UITableViews are created, and their cells are set...

Array of UITableViews declared at beginning of .m file:

UITableView* channel_tableView[ TOTAL_TX_CHANNELS ];

In function viewDidLayoutSubviews I create the UITableView:

 for( int channel=0; channel < TOTAL_TX_CHANNELS; ++channel ){  
CGRect tableFrame = CGRectMake(x, y, width, height);

channel_tableView[ channel ] = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];

channel_tableView[ channel ].scrollEnabled = YES;
channel_tableView[ channel ].userInteractionEnabled = YES;
channel_tableView[ channel ].bounces = YES;

channel_tableView[ channel ].delegate = self;
channel_tableView[ channel ].dataSource = self;

channel_tableView[ channel ].autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[channel_tableView[ channel ]  reloadData];             // display channel's TableView
[[self view] addSubview:   channel_tableView[ channel ]];

}

HERE IS WHERE EACH CELL IS SET.......................

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog( @"cellForRowAtIndexPath");
    NSLog( @"   %d  = row", indexPath.row  );



    UITextView*  test_textView;
    UITextView*  test_2nd_textView;
    UITextView*  test_3rd_textView;


    // Determine which channel_tableView:
        int channel;
        for( channel = 0; channel < TOTAL_TX_CHANNELS; ++channel )
        {
            if( tableView == channel_tableView[ channel ] )
                break;
        }
        // channel = tableView's channel
        NSLog( @"   %d  = channel", channel  );

    // DOCUMENTATION:  Table View Programming Guide for iOS > Adding subviews to a cellís content view

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSLog( @"       cell nil");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
        /* Though it's UITableViewCellStyleDefault, the three defaults (image, label, detail label) are nil
        if not set. */
    }
        // Add UITextView for channel pipe to cell:
            int pipe_width = 20;
            int w = pipe_width;
            int x = channel_tableView_width/2 - pipe_width/2;
            int h = channel_row_height;
            int y = 0;
            test_textView = [[UITextView alloc] initWithFrame:CGRectMake    (x,y, w,h)] ;
            [test_textView setFont:[UIFont boldSystemFontOfSize:8.0]];
            [test_textView setEditable:NO];
            [test_textView setTextAlignment:NSTextAlignmentCenter];
            // Round the corners and set border color
            if(channel == power_channel )
                [test_textView setBackgroundColor:[UIColor greenColor]];
            else
                [test_textView setBackgroundColor:[UIColor whiteColor]];
            [[test_textView layer] setBorderColor:[[UIColor blackColor] CGColor]];
            [[test_textView layer] setBorderWidth:1];
            //[[test_textView layer] setCornerRadius:15];
            [test_textView setClipsToBounds: YES];


        // Add UITextView for PWR RX to cell:
        int PWR_RX_width = channel_tableView_width/2;
         y = y + h ;
         w = PWR_RX_width;
         x = channel_tableView_width/2 - PWR_RX_width/2;
         h = 20;
            test_2nd_textView = [[UITextView alloc] initWithFrame:CGRectMake    (x,y, w,h)];
            [test_2nd_textView setFont:[UIFont boldSystemFontOfSize:8.0]];
            [test_2nd_textView setEditable:NO];
            [test_2nd_textView setTextAlignment:NSTextAlignmentCenter];
            // Round the corners and set border color
            [test_2nd_textView setBackgroundColor:[UIColor whiteColor]];
            [[test_2nd_textView layer] setBorderColor:[[UIColor blackColor] CGColor]];
            [[test_2nd_textView layer] setBorderWidth: 1];
            //[[test_2nd_textView layer] setCornerRadius:15];
            [test_2nd_textView setClipsToBounds: YES];      

        // Add UITextView for device to cell:
        int device_width = channel_tableView_width/2;
         y = y + h-3;
         w = device_width;
         x = channel_tableView_width/2 - device_width/2;
         h = 40;
            test_3rd_textView = [[UITextView alloc] initWithFrame:CGRectMake    (x,y, w,h)];
            [test_3rd_textView setFont:[UIFont boldSystemFontOfSize:8.0]];
            [test_3rd_textView setEditable:NO];
            [test_3rd_textView setTextAlignment:NSTextAlignmentCenter];
            // Round the corners and set border color  
            [test_3rd_textView setBackgroundColor:[UIColor whiteColor]];
            [[test_3rd_textView layer] setBorderColor:[[UIColor blackColor] CGColor]];
            [[test_3rd_textView layer] setBorderWidth: 1];
            //[[test_3rd_textView layer] setCornerRadius:15];
            [test_3rd_textView setClipsToBounds: YES];

        /*
                test_textView.tag = TEST_TEXTVIEW_TAG;
                test_2nd_textView.tag = TEST_2ND_TEXTVIEW_TAG;
                test_3rd_textView.tag = TEST_3RD_TEXTVIEW_TAG;

        test_textView     = (UITextView*)[cell.contentView viewWithTag: TEST_TEXTVIEW_TAG];
        test_2nd_textView = (UITextView*)[cell.contentView viewWithTag: TEST_2ND_TEXTVIEW_TAG];
        test_3rd_textView = (UITextView*)[cell.contentView viewWithTag: TEST_3RD_TEXTVIEW_TAG];
         */
    //[test_textView setText:     [NSString stringWithFormat: @"pipe-%d", indexPath.row ]];
    [test_2nd_textView setText: [NSString stringWithFormat: @"PWR RX %d", indexPath.row ]];
    [test_3rd_textView setText: [NSString stringWithFormat: @"device %d", indexPath.row ]];  
            // Add created UI objects to cell[ indexPath.row ]:
                [cell.contentView addSubview:test_textView ];
                [cell.contentView addSubview:test_2nd_textView ];
                [cell.contentView addSubview:test_3rd_textView ];
    return cell;
}
like image 608
Doug Null Avatar asked May 25 '13 04:05

Doug Null


People also ask

Is UITableView scrollable?

Smooth Scrolling:- As a result, it affects the smoother scrolling experience. In UITableVIew, Scrolling will be smooth through cell reuse with the help of prepareForReuse() method.

What is tableView?

A table view displays a single column of vertically scrolling content, divided into rows and sections. Each row of a table displays a single piece of information related to your app. Sections let you group related rows together. For example, the Contacts app uses a table to display the names of the user's contacts.

Is it possible to add UITableView within a UITableViewCell?

yes it is possible, I added the UITableVIew within the UITableView cell .. :) no need to add tableview cell in xib file - just subclass the UITableviewCell and use the code below, a cell will be created programatically.


2 Answers

you need to define your CellIdentifier like Bellow:-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = @"Test";
    cell.textLabel.textColor=[UIColor whiteColor];
    return cell;
}

NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];

this may help you

like image 141
Nitin Gohel Avatar answered Nov 15 '22 19:11

Nitin Gohel


I had the same issue but for me it was the properties of the elements within the UITableviewCell. I suggest that you use a custom cell and create the textfields with the NIB or StoryBoard. Then create outlets for them in the custom cell class with the following signature:

@property (strong, nonatomic) IBOutlet UITextField *myText;

The important part is to use the strong and not the weak property.

Here is a link to my post.

Good luck and hope this help!

like image 40
RobertH Avatar answered Nov 15 '22 17:11

RobertH