2013-05-28 56 views
2

我正在使用故事板,因爲我有一個自定義表格。我需要在單元格內的標籤上顯示值。我試着爲它創建一個IBOutlet,但它似乎不接受它,它給了我一個錯誤,說「連接」xyz「不能有一個原型對象作爲其目的地」將動態文本設置爲故事板中的uilabel

回答

2

找到的解決方案,我動態創建一個標籤和設置值到它 `如果(indexPath.row == 2){ 靜態 *的NSString將Identifier1 = @ 「小區3」;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier1]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier1]; 
     cell.textLabel.lineBreakMode=NSLineBreakByCharWrapping; 
     cell.textLabel.numberOfLines = 0; 
     cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0]; 

    } 

    UILabel *txtDate = [[UILabel alloc] initWithFrame:CGRectMake(13.0f, 26.0f, 294.0f, 30.0f)]; 
    txtDate.text = stringFromDate; 

    [txtDate setUserInteractionEnabled:NO]; 
    txtDate.font = [UIFont fontWithName:@"Helvetica" size:15.0]; 
    [cell.contentView addSubview:txtDate]; 

    return cell; 


}` 
1

在表格視圖中有動態單元格,您無法將表視圖控制器中的IBOutlet連接到單元格內的元素,因爲單元格是原型對象。如果您將表視圖設置爲靜態單元格,這將是一個不同的情況。

What's the difference between static cells and dynamic prototypes?

因爲你使用動態的細胞,你需要繼承的UITableViewCell,創建在子細胞標籤的出口,以及它在你的故事板連接的UILabel。

如果事先知道行數不會根據數量等因素改變,那麼另一個選擇就是使用靜態表格視圖單元格。

0

的iOS 10+ &斯威夫特3+,使用此

let Identifier1 = "Cell3" 

var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(Identifier1) 
     if cell == nil { 
      cell = UITableViewCell(style: UITableViewCellStyleDefault, reuseIdentifier: Identifier1) 
      cell.textLabel.lineBreakMode = NSLineBreakByCharWrapping 
      cell.textLabel.numberOfLines = 0 
      cell.textLabel.font = UIFont(name: "Helvetica", size: 15.0) 
     } 
     var txtDate: UILabel = UILabel(frame: CGRectMake(13.0, 26.0, 294.0, 30.0)) 
     txtDate.text = stringFromDate 
     txtDate.userInteractionEnabled = false 
     txtDate.font = UIFont(name: "Helvetica", size: 15.0) 
     cell.contentView.addSubview(txtDate) 
     return cell 
相關問題