10

我有四个UITableViews并排,以编程方式创建。问题 1:第一个单元格始终为空白。问题 2:当我向下滚动时,一些单元格是空白的。当我来回滚动时,其他单元格消失。

这是UITableViews创建它们的代码,并设置它们的单元格......

UITableViews在文件开头声明的数组.m

UITableView* channel_tableView[ TOTAL_TX_CHANNELS ];

在函数viewDidLayoutSubviews中,我创建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 ]];

}

这里是每个单元格的设置.......................

- (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;
}
4

3 回答 3

14

你需要定义你CellIdentifier喜欢的波纹管: -

- (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];

这可能会帮助你

于 2013-05-25T05:20:49.300 回答
4

我有同样的问题,但对我来说这是 UITableviewCell 中元素的属性。我建议您使用自定义单元格并使用 NIB 或 StoryBoard 创建文本字段。然后使用以下签名在自定义单元类中为它们创建出口:

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

重要的部分是使用强而不是弱属性。

这是我的帖子的链接。

祝你好运,希望这有帮助!

于 2013-05-25T04:41:43.747 回答
0

您必须添加一个字典数组并在滚动 tableview 之后添加基于 indexpath.row 的 textfileds 数据。

看我的例子

 NSMutableDictionary *dicttable=[NSMutableDictionary dictionaryWithDictionary:[arrofselect     objectAtIndex:indexPath.row]];
    [dicttable setValue:[NSString stringWithFormat:@"%@",self.other_text.text] forKey:@"AnswerId"];
    [dicttable setValue:[[arrQuestions objectAtIndex:indexPath.row] valueForKey:@"QuestionID"] forKey:@"QuestionID"];
    [arrofselect replaceObjectAtIndex:indexPath.row withObject:dicttable];

你在文本编辑时替换了基于字典的 indexapth.row 标记值

  • (void)textFieldDidBeginEditing:(UITextField *)textField
  • (void)textFieldDidEndEditing:(UITextField *)textField

享受...干杯

于 2013-05-25T04:26:39.837 回答