【问题标题】:How do I embed a UITableView in a UIView?如何在 UIView 中嵌入 UITableView?
【发布时间】:2017-01-21 16:09:39
【问题描述】:

我在使用xib 构建的自定义视图中有一个UIView。我需要在上述视图中显示UITableView。起初我考虑放置一个container 并在其中嵌入一个UITableViewController。结果我不能将containers 放在xib 文件中,或者至少没有办法从IB 中做到这一点,因为它没有出现在右下角的视图部分中。

我可以通过编程方式创建UITableView 并将其添加为视图的子视图。它按预期显示,但我似乎无法在其中添加单元格。我还尝试创建一个行为良好的 UITableViewControllerstoryboard 视图关联,按如下方式实例化该控制器:

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

然后尝试访问UITableView 的插座nil。然后我在某处读到我应该做一个vc.loadView(),因为顾名思义,它会加载视图而我的IBOutlet 不会是nil。这行得通。插座的时间更长nil。但是,当我将容器视图中的表格添加为子视图时,它仍然不显示任何单元格。只有分隔线,没有内容。我已经没有想法了!

编辑

我没有任何 UITableViewCell 实现,因为表是静态的。

【问题讨论】:

    标签: ios swift uitableview uiview embed


    【解决方案1】:

    好的方法是在自定义视图中使用 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
    }
    

    希望我回答了你的问题。

    【讨论】:

    • 如果有任何区别,表是静态的。它的内容保持不变。
    • 其实我忘了提,我没有 UITableViewCell 实现,因为我的表是静态的
    【解决方案2】:

    目标 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 UITableView 数据源 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

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 2016-05-08
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2023-04-05
      相关资源
      最近更新 更多