2017-01-21 23 views
0

我在使用xib構建的自定義視圖中有UIView。我需要在上述視圖中顯示UITableView。起初我想過放置一個container並在其中嵌入UITableViewController。原來我不能將containers置於xib文件中,或者至少從IB開始無法執行,因爲它不會顯示在右下角的視圖部分。如何在UIView中嵌入UITableView?

我可以通過編程創建UITableView並將其作爲視圖的子視圖添加。它按預期顯示,但我似乎無法添加單元格。我還試圖用storyboard視圖中創建相關聯的阱表現UITableViewController,如下實例化控制器:

let storyboard = (UIStoryboard(name: "Main", bundle: nil)) let vc = storyboard.instantiateViewControllerWithIdentifier("tableViewController") as! TestTableViewController

,然後試圖訪問UITableView的出口,該出口是nil。然後我讀了一個地方,我應該做一個vc.loadView(),因爲顧名思義,它加載視圖,我的IBOutlet不會是nil。這工作。該插座更長nil。但是,當我將容器視圖中的表添加爲子視圖時,它仍然不顯示單元格。只有分隔線但沒有內容。我已經用完了想法!

編輯

我沒有任何UITableViewCell實現這些表是靜態的。

回答

0

好方法是使用UITableView的自定義視圖中:

如果添加實現代碼如下程序然後註冊使用筆尖或UITableView的子類,你的細胞像

tableView.registerNib(UINib(nibName: "UITableViewCellSubclass", bundle: nil), forCellReuseIdentifier: "UITableViewCellSubclass") 

,因爲如果你正在使用XIB創建的UITableViewCell 。

tableView.registerClass(UITableViewCellSubclass.self, forCellReuseIdentifier: "UITableViewCellSubclass") // using code. 
tableView.delegate = self 
tableView.dataSource = self 

然後使用2個必需的代表。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
return 2 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
return tableView.dequeueReusableCellWithIdentifier("UITableViewCellSubclass", forIndexPath: indexPath) as! UITableViewCellSubclass 
} 

希望我回答了您的問題。

+0

如果這有什麼差別,該表是靜態的。其內容保持不變。 – JadeSync

+0

其實我忘了提及,我沒有UITableViewCell實現,因爲我的表是靜態的 – JadeSync

0

目標C 您需要代表你的ViewController,如果你有一個視圖控制器,把表的委託:

的UIViewController 的UITableViewDelegate UITableViewDataSource

而且你可以用你的功能

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; //count of section 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [catagorry count]; //count number of row from counting array hear cataGorry is An Array 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView 
      cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *MyIdentifier = @"MyIdentifier"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

     if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
              reuseIdentifier:MyIdentifier] autorelease]; 
     } 

     // Here we use the provided setImageWithURL: method to load the web image 
     // Ensure you use a placeholder image otherwise cells will be initialized with no image 
     [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"] 
         placeholderImage:[UIImage imageNamed:@"placeholder"]]; 
      cell.textLabel.text = @"My Text"; 
     return cell; 
    } 

Swift:

代表: 的UIViewController UITableViewDataSource 的UITableViewDelegate

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.delegate = self 
    tableView.dataSource = self 
} 

斯威夫特

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) 

    let row = indexPath.row 
    cell.textLabel?.text = swiftBlogs[row] 

    return cell 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) 

    let row = indexPath.row 
    cell.textLabel?.text = swiftBlogs[row] 

    return cell 
} 


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return swiftBlogs.count 
} 


func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

看起來更More inf

相關問題