2013-05-26 34 views
0

我的UITableViews的第一行都是空白的,即使我的NSLog在函數tableView .. cellForRowAtIndexPath中顯示行0,1,2,...都是調用並初始化的。UITableView第一個單元格沒有設置

我使四個UITableView並排排列,每個都有40行。 NSLog的顯示,所有40行都調用了cellForRowAtIndexPath函數,但每個顯示行0都是空的。顯示的第1行包含我期望在第0行的單元格,第2行包含第1行的單元格等。

下面是創建UITableView數據結構的代碼以及構建其單元格的函數cellForRowAtIndexPath。

這裏有四個UITableViews在.m文件範圍級別的陣列(在其開始):

UITableView* channel_tableView[ TOTAL_TX_CHANNELS ]; 

而且這裏的地方UITableViews被創建在功能viewDidLayoutSubviews:

for(int channel=0; channel < TOTAL_TX_CHANNELS; ++channel) 
    { 
     // Create channel_tableView: 

      CGRect tableFrame = CGRectMake(x, y, width, height); 

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

      channel_tableView[ channel ].rowHeight = channel_row_height; 
      channel_tableView[ channel ].sectionFooterHeight = 0; 
      channel_tableView[ channel ].sectionHeaderHeight = 0; 
      channel_tableView[ channel ].scrollEnabled = YES; 
      //new_channel_tableView.showsVerticalScrollIndicator = 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 ]]; 
    } 

而且這裏是創建所有單元格的功能:

- (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 
    // Give each cell a cell identifier unique to each channel tableView and unique to each row, so that each gets a unique data structure: 
      NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",channel,indexPath.row]; 

    //static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     // if nil: cell(chan, row) has not been created before. <>nil: cell = data structure previously initialized 
    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 + 1 ]]; 
    [test_3rd_textView setText: [NSString stringWithFormat: @"device %d", indexPath.row + 1 ]]; 
      // 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; 
} 

SCREENSH OT顯示空白行0 ..............................

enter image description here

+0

我在下面的答案應該指向正確的方向,但我也會評論你濫用可重複使用的單元格ID。小區ID應該標識出您正在出隊的單元的類型,而不是確切的單元位置。這樣,您不必每次都重新創建UI小部件,只需設置其文本和其他相關屬性即可。 –

回答

1

test_2nd_textViewtest_3rd_textView是被繪製在第一個表格單元格之外(這不是剪裁),因爲您正在使用y>=cell_height框架初始化它們。對於test_textView,y=0, h=cell_height。對於test_2nd_textView,y=cell_heighth=20,因此test_2nd_textView繪製在第二個單元格的頂部。

+0

謝謝布蘭登!這解決了它。我必須失明! :) –

相關問題