【发布时间】:2011-03-03 14:37:43
【问题描述】:
我有一个由 Interface Builder 在黑色背景中创建的简单 MainWindow 文件。在这个简单的视图中,我从上到下添加了一个小视图 (300x100)、一个 textLabel 和几个按钮。
在我定义的 MainApplication.h 中:
IBOutlet RoundRectView* smallView; IBOutlet UILabel* 标签;
在 MainApplication.h 中,我 @synthesized 这些变量并使用它:
[label setText:@"Test"];
我可以在屏幕上看到所有内容,而且效果很好。
现在我的问题:
我想在我的白色小视图中添加一个有两行的表格,我所做的是:
- 创建一个 tableController.h 作为委托和数据源:
@interface 收入支出控制器: UITableViewController { IBOutlet UITableView* 小表; NSArray *内容; }
@property(非原子,保留) UITableView *smallTable; @财产 (非原子,保留)NSArray *content;
@结束
- 使用以下命令创建 .m 文件:
@synthesize smallTable; @合成 内容;
(void)viewDidLoad { [超级视图DidLoad];内容 = [[NSArray 分配] initWithObjects:@"Item1", @"Item2", 零]; }
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // 返回部分的数量。 返回0; }
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 返回节中的行数。 返回 2; }
// 自定义表格的外观 查看单元格。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 重用标识符:细胞标识符] 自动释放]; } NSLog(@"单元格内");
// Configure the cell... cell.textLabel.text = [contentobjectAtIndex:indexPath.row];
return cell; }
现在在 Interface Builder 中,我将 UITableView 拖放到白色小视图中,并将 Table View Controller 与 File Owner 的窗口一起拖放,然后进行以下连接:
Table View Controller 到 tableController 的类
网点: 数据源和委托给我的表控制器
参考网点: customTable 和 View 到我的表控制器
当我执行时,我可以看到表格视图的行,但没有内容,我也看不到 NSLog,但如果我将 NSLog 放在 viewDidLoad 中,我可以看到它。
我真的很陌生(2 天),任何帮助都将不胜感激。
谢谢!
【问题讨论】:
-
已解决。首先,TableView 是分组的,所以我必须返回几乎一个部分。其次,在 Interface Builder 中,我必须将表中的“视图”链接到我的外部控制器。现在它完美地工作了。
标签: iphone