如何将静态 TableView 添加到 ViewController [英] How to add static TableView to ViewController

查看:28
本文介绍了如何将静态 TableView 添加到 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用容器和 UITableViewController 的情况下向 ViewController 添加静态 TableView?

How to add static TableView to ViewController without using of a container and UITableViewController?

我认为前一段时间是可能的,但现在使用最新的 xCode 故事板和 iOS 7 就不行了.没有有效答案的类似问题在这里 - iOS 7 将 UIView 更改为 UITableView我的表有一点不同 - 我尝试添加静态数据.我在程序启动时遇到应用程序崩溃.如果我只设置数据源,程序就会崩溃.如果我取消链接数据源并保留委托和出口,则程序将在没有警告的情况下启动,但单元格为空.备注 - 在没有我帮助的情况下,Storyboard 在视图树中将 ViewController 的命名从 ViewController 更改为 TableViewController.我认为它是在添加 IBOutlet UITableView *myTableView 之后发生的.为什么会这样?这是什么意思?

I think it was possible some time ago but now with latest xCode storyboard and iOS 7 it is not. The similar question without valid answer is here - iOS 7 change UIView to UITableView My table has a little difference - I try to add a static data. And I get an application crash at program start. Program crashes if I set just the datasource. If I unlink the datasource and stay the delegate and outlet the program starts without warnings but with empty cells. A remark - Storyboard changes naming for ViewController from ViewController to TableViewController at view tree without my assistance. I think it occurs after adding an IBOutlet UITableView *myTableView. Why so and what does it mean?

推荐答案

在 UITableViewDataSource 中有 2 个必需的方法:

In the UITableViewDataSource there are 2 required methods :

– tableView:cellForRowAtIndexPath:  required method
– tableView:numberOfRowsInSection:  required method

这意味着在 MYTableViewController 中你必须实现像这样的(示例有 3 个单元格 Test1、Test2 和 Test3):

It means that in MYTableViewController you must implement those like this (example with 3 cells Test1, Test2 and Test3) :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyStaticCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Test%d",indexPath.row+1];
    return cell;
}

这篇关于如何将静态 TableView 添加到 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆