2011-10-26 44 views
4

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

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

+0

“UITableView无法正常工作”.... *什么是不工作? –

+0

首先,当我只是将UITableView添加到我的xib并构建时,我尝试滚动tableview而没有任何反应! – Mateus

+0

你能分享一些代码吗?具体来说,tableview数据源和委托回调? – bryanmac

回答

4

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

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

右键单击(或按住Ctrl并单击)并从您的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。