如何在 Xcode 4.2 上为 IOS 5 创建 UITableView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7910206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:14:36  来源:igfitidea点击:

How can i create an UITableView on Xcode 4.2,for IOS 5?

iphonec++xcodeuitableviewios5

提问by Mateus

Last week i have downloaded Xcode 4.2, so when i started building apps i've tried to add an UITableViewto one of my projects (as normal as i have been doing since i began developing) but the UITableViewisn't working. I've searching for tutorials but i didn't found any so: How can i create an UITableView on Xcode 4.2,for IOS 5?

上周我下载了 Xcode 4.2,所以当我开始构建应用程序时,我试图将一个添加UITableView到我的一个项目中(就像我开始开发以来所做的那样正常),但UITableView它不起作用。我一直在寻找教程,但我没有找到任何教程:如何在 Xcode 4.2 上为 IOS 5 创建 UITableView?

obs:I'm not using storyBoard just the XIB's!

obs:我不使用storyBoard 只是XIB 的!

采纳答案by AMayes

in your .h file, add the following:

在您的 .h 文件中,添加以下内容:

@interface YourClass: UIViewController <**UITableViewDataSource, UITableViewDelegate**>

right-click (or ctrl-click) and drag from your tableView to the File's Owner twice. Once, select "delegate", and once select "dataSource".

右键单击(或按住 ctrl 单击)并从 tableView 拖动到 File's Owner 两次。一次,选择“delegate”,一次选择“dataSource”。

Then, in your .m file, you need to implement the following:

然后,在您的 .m 文件中,您需要实现以下内容:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return 1;}

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

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [[cell textLabel] setText:yourText];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //do what you want to with the information at indexPath.row
}

That should get you a working tableView.

那应该会给你一个工作表视图。

回答by teriiehina

You should start with the project template "Master-Detail Application" and look at the mechanisms to create your own code in C++.

您应该从项目模板“主从应用程序”开始,并查看在 C++ 中创建您自己的代码的机制。

But at the core, creating a UITableView is a two step:

但从本质上讲,创建 UITableView 是两步:

  • init the view
  • plug it to its delegate/datasource
  • 初始化视图
  • 将其插入其委托/数据源

also, this might help you: Can i write Cocoa Touch[iPhone] applications in C++ language

另外,这可能对您有帮助:我可以用 C++ 语言编写 Cocoa Touch[iPhone] 应用程序吗?