2012-11-05 59 views
0

我在創建UITableView時遇到了問題,因爲我無法在其中一個單元格中看到標籤和步進器,我分配給它。一個單元格中的標籤不顯示在iPhone的tableView中

這裏是代碼,我希望有人可以找出問題。當它到達情況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; 
} 
+0

這是一個糟糕的'cellForRowAtIndexPath'實現,在重用單元格之後至少會有一個添加的視圖,您需要重新組織方法以使子視圖層次清晰。至於'案例4:',您使用的是白色文本顏色,可能在默認的白色背景上不可見。 –

+0

是單元格背景顏色和lblVarsta背景顏色相同? – AppleDelegate

+0

AppleDelegate和A-Live非常感謝您的建議。現場可以請給我一些提示,也許是一個網站或什麼,因爲我是一個初學者。我很感謝你的幫助。 – alin

回答

0

您有

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

和 lblVarsta = [[ALLOC的UILabel] 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)]; 

你得子視圖同時添加到內容查看,並在其框架必須是這樣的,他們都可以出現。

+0

Srikanth謝謝你!現在我有另一個問題。我在這裏寫它http://stackoverflow.com/questions/13238730/how-to-align-the-segmented-control-at-the-right-of-the-cell-of-the-tableview。也許你也可以弄清楚。祝你今天愉快! – alin

+0

我想出了問題。我正在重新定義對象並丟失了分段控件的座標。 – alin

相關問題