普通视图控制器内的表视图 [英] Table view inside a normal view controller
问题描述
我正在做这个 iPhone 项目,我需要在一个普通的视图控制器中有一个(动态)表视图.我没有选择表视图控制器,因为我需要在页面中放置其他东西.想象一下一个包含文本字段、按钮和我的大约 4-5 个单元格的小表格视图的页面.
I'm doing this iPhone project where I need to have a (dynamic) table view inside a normal view controller. I didn't choose a Table View Controller because I need to put other stuff in the page. Imagine a page with text fields, buttons and my small table view of about 4-5 cells.
当我运行应用程序时,我需要触摸一个按钮才能转到此视图.我点击按钮,应用程序崩溃并告诉我:
When I run the app, I need to touch a button to segue to this view. The I click the button, the app crashes and tells me that:
2012-07-22 14:40:57.304 多少?[3055:f803] * 断言失败-[UITableView _createPreparedCellForGlobalRow:withIndexPath:],/SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
2012-07-22 14:40:57.304 How Many?[3055:f803] * Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
这是我的 .H 文件:
This is my .H file:
#import <UIKit/UIKit.h>
@interface ProjectViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end
这是我的 .M 文件:
This is my .M file:
#import "ProjectViewController.h"
@interface ProjectViewController ()
@end
@implementation ProjectViewController
//MyViewController.m
#pragma mark - Table View Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"numberOfSectionsInTableView");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"numberOfRowsInSection");
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
NSLog(@"cellForRowAtIndexPath");
cell.textLabel.text = @"Test";
return cell;
}
@end
我从表格视图控制拖动到我的控制器以设置委托和数据源.
I control-dragged from the table view to my controller to set the delegate and the datasource.
我做错了什么??
感谢您的帮助.
推荐答案
尝试在 .h
文件中创建一个插座,并将其连接到 tableView
中的 .h
代码>故事板代码>
Try to make an outlet in your .h
file and wire it to the tableView
in your storyboard
ProjectViewController.h
@property (nonatomic, strong) IBOutlet UITableView *myTableView;
ProjectViewController.m
@synthesize myTableView;
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");
UITableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:@"cell"];
NSLog(@"cellForRowAtIndexPath");
cell.textLabel.text = @"Test";
return cell;
}
这篇关于普通视图控制器内的表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!