且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式创建UITableViewController

更新时间:2023-02-20 20:51:31

问题是你正在创建一个UITableViewController,它是一个UIViewController,并且预计会在导航堆栈上。你想要做的是创建一个UITable,这是一个UIView。您也没有设置表的委托和数据源,您需要这样做以获得回溯。

The problem is that you're creating a UITableViewController, which is a UIViewController, and will expect to be on the nav stack. What you want to do is create a UITableView, which is a UIView. You are also not setting the table's delegate and data source, which you will need to do to get calbacks.

您的viewDidLoad应该看起来像这样

Your viewDidLoad should look something like this

- (void)viewDidLoad 
{
    [super viewDidLoad];
    UITableView *table = [[[UITableView alloc] initWithStyle:UITableViewStylePlain] autorelease];
    table.dataSource = self;
    table.delegate = self;
    table.frame = CGRectMake(0, 10, 320, 100);
    [self.view addSubview:table];
}

(请注意,如果您需要访问以外的表格回调,你应该把它保存在一个ivar而不是在本地声明它,并且应该保留它。如果你需要更多行代码来告诉我你的意思,请告诉我。)

(Note that if you're going to need access to the table outside of the callbacks, you should save it in an ivar rather than declaring it locally, and should retain it. Let me know if you need a few more lines of code to show you what I mean)