Objective c TableView数据在模拟器上不可见
我使用TableView来显示数据数组,但在编译应用程序时,似乎没有调用TableView的函数。以下是我为此目的使用的代码:Objective c TableView数据在模拟器上不可见,objective-c,ios,ipad,Objective C,Ios,Ipad,我使用TableView来显示数据数组,但在编译应用程序时,似乎没有调用TableView的函数。以下是我为此目的使用的代码: - (NSInteger)tableView:(UITableView *)TableView numberOfRowsInSection:(NSInteger)section { return listdata.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForR
- (NSInteger)tableView:(UITableView *)TableView numberOfRowsInSection:(NSInteger)section {
return listdata.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"myCell"];
if (cell == nil) {
cell = [[ UITableViewCell alloc ] initWithFrame:CGRectZero];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.listdata objectAtIndex:row];
return cell;
}
如果要使用
UITableViewController
,而只是在UIViewController
中添加UITableView
,则需要将TableView的委托和数据源设置为VC的类
您可以在故事板中执行此操作,方法是按住ctrl键从TableView拖动到ViewController并选择“委托/数据源”,或者通过将此代码放在ViewController的.m中的viewDidLoad
方法以编程方式执行此操作:
_tableView.delegate = self;
_tableView.datasource = self;
并将
添加到您的ViewController中。h:
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDatasource>
@接口MyViewController:UIViewController
是否已将数据源和委托设置到tableview?打印控制台,位于numberOfRowsInSection委托,NSLog(@“count%d”,listdata.count);什么是listdata.count????它是一个NSArray吗???是的,请参见[self.listdataobjectatindex:row];