0

我在创建 时遇到问题UITableView,因为我在其中一个单元格中看不到我分配给它的标签和步进器。

这是代码,我希望有人能解决问题。当它到达案例 4 时,它不会在单元格中显示 lblVarsta,只显示步进器。我调试了它,值在那里,但是看不到带有该值的标签。谢谢!

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *inputField;

lblRasa = [[UILabel alloc]initWithFrame:CGRectMake(120, 12, 185, 30)];
lblCuloare = [[UILabel alloc]initWithFrame:CGRectMake(120, 12, 185, 30)];
lblJudet = [[UILabel alloc]initWithFrame:CGRectMake(120, 12, 185, 30)];
lbldate = [[UILabel alloc] initWithFrame:CGRectMake(120, 12, 185, 30)];

inputField = [[UITextField alloc]initWithFrame:CGRectMake(120, 12, 185, 30)];
inputField.adjustsFontSizeToFitWidth = YES;
inputField.borderStyle = UITextBorderStyleRoundedRect;
inputField.tag = indexPath.row;
inputField.delegate = self;


segmentedControl1 = [[UISegmentedControl alloc] initWithFrame:CGRectMake(220, 10, 100, 10)];
segmentedControl2 = [[UISegmentedControl alloc] initWithFrame:CGRectMake(220, 10, 100, 10)];

NSArray * segmentItems = [NSArray arrayWithObjects: @"Da", @"Nu", nil];


 //    UIDatePicker *pv = [[UIDatePicker alloc] init];
 //    UIActionSheet *uas = [[UIActionSheet alloc] initWithTitle:@"pv" delegate:self      cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
 //    
 //    [uas addSubview:pv];

if (!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellAccessoryNone reuseIdentifier:CellIdentifier];

}

switch ([indexPath row])
{
    case 0:
        cell.textLabel.text = @"Nume";
        inputField.text = dog?dog.title:@"";
        [cell addSubview:inputField];
        break;
    case 1:
        cell.textLabel.text = @"Rasa";
        lblRasa.text = dog?dog.rasa:@"";
        [cell addSubview:lblRasa];
        break;
    case 2:
        cell.textLabel.text = @"Culoare";
        lblCuloare.text = dog?dog.culoare:@"";
        [cell addSubview:lblCuloare];
        break;
    case 3:{
        cell.textLabel.text = @"Data nasterii";
        lbldate.text = dog?dog.data_nas:@"";

        [cell addSubview:lbldate];
        [cell addSubview:actionSheet];
    break;}
    case 4:{
        lblVarsta = [[UILabel alloc] initWithFrame:CGRectMake(120, 12, 185, 30)];
        stepper = [[UIStepper alloc] initWithFrame:CGRectMake(220, 10, 100, 10)];
        cell.textLabel.text = @"Varsta";
        //inputField.text = dog?[NSString stringWithFormat:@"%d",dog.varsta]:@"";

        lblVarsta.text = @"3 ani";
       [stepper addTarget:self action:@selector(stepperValueChanged:) forControlEvents:UIControlEventValueChanged];


       [lblVarsta setTextColor:[UIColor whiteColor]];
       [lblVarsta setBackgroundColor:[UIColor clearColor]];
        //[lbl5 setTextAlignment:UITextAlignmentLeft];

        [stepper setStepValue:10];

        [cell addSubview:lblVarsta];
        [cell.contentView addSubview:stepper];
        break;}
    case 5:
        cell.textLabel.text = @"Proprietar";
        inputField.text = dog?dog.proprietar:@"";
        [cell addSubview:inputField];
        break;
    case 6:
        cell.textLabel.text = @"Judet";
        lblJudet.text = dog?dog.judet:@"";
        [cell addSubview:lblJudet];
        break;
    case 7:
        cell.textLabel.text = @"Adresa";
        inputField.text = dog?dog.adresa:@"";
        [cell addSubview:inputField];
        break;
    case 8:{
        cell.textLabel.text = @"Parinti";
        //inputField.text = dog?dog.parinti:@"";
        //lblVarsta.text = [NSString stringWithFormat:@"%.f", stepper.value];

        segmentedControl1 = [[UISegmentedControl alloc] initWithItems: segmentItems];
  //            segmentedControl1.segmentedControlStyle = UISegmentedControlStyleBar;
  //            segmentedControl1.selectedSegmentIndex = 0;

        [segmentedControl1 addTarget:self action:@selector(segmentedControlIndexChanged:) forControlEvents:UIControlEventValueChanged];

        [cell.contentView addSubview:segmentedControl1];
        break;}
    case 9:
        cell.textLabel.text = @"Pedigree";
        segmentedControl1 = [[UISegmentedControl alloc] initWithItems: segmentItems];
        [segmentedControl1 addTarget:self action:@selector(segmentedControlIndexChanged:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:segmentedControl2];
        break;
}

return cell;
 }
4

1 回答 1

0

你有

    [cell addSubview:lblVarsta];
    [cell.contentView addSubview:stepper];

和 lblVarsta = [[UILabel alloc] initWithFrame:CGRectMake(120, 12, 185, 30)]; stepper = [[UIStepper alloc] initWithFrame:CGRectMake(220, 10, 100, 10)];

你为什么不试试

     [cell.contentView addSubview:stepper];
     [cell.contentView addSubview:lblgarsta];

框架如下

    lblVarsta = [[UILabel alloc] initWithFrame:CGRectMake(120, 12, 100, 30)];
    stepper = [[UIStepper alloc] initWithFrame:CGRectMake(220, 10, 100, 10)];

您必须将两个子视图都添加到内容视图中,并且它们的框架必须使它们都可以出现。

于 2012-11-05T20:54:51.423 回答