簡體   English   中英

如何在 UIView 中嵌入 UITableView?

[英]How do I embed a UITableView in a UIView?

我在使用xib構建的自定義視圖中有一個UIView 我需要在上述視圖中顯示一個UITableView 起初我想放置一個container並在其中嵌入一個UITableViewController 結果我不能將containers放在xib文件中,或者至少沒有辦法從IB中做到這一點,因為它沒有顯示在右下角的視圖部分。

我可以以編程方式創建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實現,因為表格是靜態的。

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

如果您以編程方式添加 tableview,則使用 Nib 或 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
}

希望我回答了你的問題。

目標 c 您需要在視圖控制器中使用委托,如果您有視圖控制器,請放置表的委托:

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

斯威夫特:

委托: 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
}

查看更多更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM