我如何在Xcode 4.2上创建一个UITableView,用于IOS 5

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

本文关键字:一个 UITableView IOS 用于 Xcode 创建      更新时间:2023-10-16

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

obs:我没有使用storyBoard只是XIB的!

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

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

右键单击(或ctrl-click)并从tableView拖动到文件的所有者两次。一次选择"委托",一次选择"数据源"。

然后,在您的.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
}

这会给你一个工作的tableView

您应该从项目模板"Master-Detail Application"开始,并查看在c++中创建自己的代码的机制。

但在核心,创建一个UITableView是两个步骤:

  • 初始化视图
  • 将其插入其委托/数据源

同样,这可能会帮助你:我可以用c++语言写Cocoa Touch[iPhone]应用程序吗