上周我下载了 Xcode 4.2,所以当我开始构建应用程序时,我尝试将 UITableView
添加到我的一个项目中(就像我开始开发以来所做的一样)但是 UITableView
不工作。我一直在寻找教程,但没有找到:
我如何在 Xcode 4.2 上为 IOS 5 创建一个 UITableView?
obs:我没有使用 Storyboard,只是 XIB 的!
最佳答案
在您的 .h 文件中,添加以下内容:
@interface YourClass: UIViewController <**UITableViewDataSource, UITableViewDelegate**>
右键单击(或按住 ctrl 键单击)并从您的 tableView 拖动到 File's Owner 两次。一次,选择“delegate”,一次选择“dataSource”。
然后,在您的 .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。
关于iphone - 我如何在 Xcode 4.2 上为 IOS 5 创建一个 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7910206/